using wildcard
-
Tuesday, August 22, 2006 9:48 AM
alter
procedure sp_ADsearch @tbname varchar(1000),@searchproduct nvarchar(500)as
declare
@sqlstr nvarchar(1000)set
@sqlstr='select * from 'set
@sqlstr= @sqlstr + @tbnameset
@sqlstr= @sqlstr + ' where name like 'set
@sqlstr= @sqlstr + '%' + @searchproduct + '%'exec
sp_executesql @sqlstr--
exec
sp_ADsearch product,eewhen executing the procedure i am getting an error "Incorrect syntax near 'ee'.".
whats wrong with the syntax? how to use % along with variables
All Replies
-
Tuesday, August 22, 2006 11:41 AMReplace the following SQL statement SET @sqlstr=@sqlstr + '%' + @searchproduct +'%' with SET @sqlstr=@sqlstr + '''%''' + @searchproduct + '''%'''. And execute your stored procedure as EXEC sp_APsearch 'product', 'ee'
-
Tuesday, August 22, 2006 11:49 AM
That results in
Where name like %ee%
And it should be
Where name like '%ee%'
-
Tuesday, August 22, 2006 11:54 AM
hi a_shyam41,
i tried myself and got the correct code for it. once again u just check u r code. its not working.
here is the right one.
alter
procedure sp_ADsearch (@tbname varchar(1000),@searchproduct varchar(500), @debug bit = 1 )as
declare
@sqlstr nvarchar(1000)set
@sqlstr='select * from 'set
@sqlstr= @sqlstr + @tbnameset
@sqlstr= @sqlstr + ' where name like '+ '''%' + @searchproduct + '%'''exec
sp_executesql @sqlstrIF
@debug = 1 PRINT @sqlstr---------------------------
exec
sp_ADsearch product,ee--*************************
no need to give product & ee in quotes. My code is working fine for me.
-
Tuesday, August 22, 2006 11:57 AM

