soo redicilous prob with escape charachters

mad_girl's Avatar, Join Date: Aug 2007
Go4Expert Member
the problem is i wanna make this piece of code work and i don know what the problem is , i think it's with the combination of charachters ' and "
Code:
set @query=@query+ "cast(ID as varchar) like'%'+ltrim(@id)+'%' " 
set @query=@query+ @option+" userName like'%'+ltrim(@name)+'%' "
set @query=@query+ @option+" cast(referrer as varchar) like'%'+ltrim(@ref)+'%' "
set @query=@query+ @option+" age=cast(@age as int) "
exec(@query)
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
That looks scary!
0
sql-programs's Avatar, Join Date: Oct 2009
Go4Expert Member
Try this query,

Code:
set @query=@query+ 'cast(ID as varchar) like ''%'+ltrim(@id)+'%'''
set @query=@query+ @option+' userName like ''%' +ltrim(@name)+'%'''
set @query=@query+ @option+' cast(referrer as varchar) like ''%'+ltrim(@ref)+'%'''
set @query=@query+ @option+' age=cast(@age as int) '