SQl Search StoredProcedure on columns FirstName,LastName,EmailID
Create procedure SearchCustomers
(
@firstname varchar(50),
@LastName varchar(50),
@EmailId varchar(50)
)
As
Begin
declare @SearchString varchar(1000)
declare @BuildQuery varchar(50)
Set @SearchString = 'Select * From Customers'
if(@firstname <> '')
Begin
set @BuildQuery = ' FirstName like ''%' + @firstname + '%'''
End
if(@Lastname<>'')
Begin
IF(@BuildQuery<>'')
set @BuildQuery = @BuildQuery + ' and LastName like ''%'+ @LastName +'%'''
Else
set @BuildQuery = @BuildQuery + ' LastName like ''%'+ @LastName +'%'''
END
if(@EmailId<>'')
Begin
if(@BuildQuery<>'')
set @BuildQuery = @BuildQuery +' and EmailId like ''%'+ @EmailId +'%'''
Else
set @BuildQuery = @BuildQuery +' EmailId like ''%'+ @EmailId +'%'''
end
IF(@BuildQuery<>'')
Begin
Set @SearchString = @SearchString + ' where' + @BuildQuery
End
Exec(@SearchString)
END
No comments:
Post a Comment