sp_executesql with parameter for LIKE expression

  • Hi,

    Is it possible to send a parameter for a LIKE expression to sp_executesql?

    E.g.

    Declare @paramdef nvarchar(500)

    Declare @sql nvarchar(2000)

    Set @paramdef = N'@ss varchar(100)

    Set @sql = 'Select * from Employees Where Name LIKE '%@ss%'

    sp_executesql @sql,@paramdef,@ss='partialname'

    I've tried all combinations of setting that parameter, but it doesnt seem to work.

    I want it so that the search is for any match,i.e. using %.

    Thanks in advance

    John

  • Nevermind, figured it out. I"ll wrap the search param with % before suppling sp_executesql.

  • kamau.john (9/17/2009)


    Nevermind, figured it out. I"ll wrap the search param with % before suppling sp_executesql.

    Or you can wrap the param in the dynamic code.

    Set @sql = 'SET @ss = ''%'' + @ss + ''%'';Select * from Employees Where Name LIKE @ss;'

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply