If I use this simple select statement, it works:
SELECT * FROM @table
WHERE [A] LIKE '%&%' OR
[A] LIKE '%''%'
But I get an error if I try:
DECLARE @SourceTable nvarchar(50) = '@table',
@ColumnName nvarchar(50) = 'A'
EXEC('SELECT * FROM '+ @SourceTable +
' WHERE ['+ @ColumnName + '] LIKE ''%&%'' OR ['+
@ColumnName + '] LIKE ''%''%''')
I get this error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '%'.