Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Dynamic SQL and using LIKE ('%' @name '%')

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-01-06 : 10:20:18
Hey all

Its coming to the end of the day and I cannot seem to be able to work out how to get the syntax right when using the LIKE function in dynamic sql. Can someone please have a look and tell me what I am doing wrong:

This is a somewhat shortened and simplified part of the query (and I cannot even get that to work):


declare @sql varchar(2000)
declare @recipientName varchar(20)

set @recipientName = 'John'

SET @sql = 'select * from DOcumentRecipient where DocumentRecipientName LIKE (''%''' + @recipientName + '''%'')'

exec (@sql)


Error Message:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'John'.


Thank you

Hearty head pats

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-01-06 : 10:24:49
Sorry sorry sorry

Would you believe it, after banging my head against a wall, as soonas a write out the problem, I solve it! Doh!

I have been putting in additional quotes between % and @param.

Hearty head pats
Go to Top of Page
   

- Advertisement -