Using LIKE in sp_executesql

眉间皱痕 提交于 2019-12-05 06:23:06

The following works for me

declare @name varchar(50)

set @name = 'WAITFOR'


exec sp_executesql 
  N'select * from sys.messages WHERE text  LIKE ''%'' + @name + ''%''', 
  N'@name varchar(50)',
  @name=@name

I think the problem must lie elsewhere.

Edit: Following your update you need to use

exec sp_executesql 
  N'SET @name = ''%'' + @name + ''%'';
    SELECT *
    FROM Tbl_Persons WHERE 1 = 1  AND lastname LIKE @name', 
  N'@name nvarchar(50)',
  @name=@name

As it stands you are searching for text containing the actual substring @name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!