如果我使用这个简单的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 '%'.