动态SELECT检查具有撇号

时间:2017-07-19 15:26:55

标签: sql sql-server tsql

如果我使用这个简单的select语句,它可以工作:

SELECT * FROM @table
WHERE [A] LIKE '%&%' OR 
[A] LIKE '%''%'

但如果我尝试,我会收到错误:

DECLARE @SourceTable nvarchar(50) = '@table',
@ColumnName nvarchar(50) = 'A'

EXEC('SELECT * FROM '+ @SourceTable +
' WHERE ['+ @ColumnName + '] LIKE ''%&%'' OR ['+
@ColumnName + '] LIKE ''%''%''')

我收到此错误:

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '%'.

0 个答案:

没有答案