I’m trying to do something similar to this question. The answer recommends using quotename inside of an exec.
Similar to this:
declare @var nvarchar(128)
set @var = 'hello world'
exec('print ''' + quotename(@var) + '''')
But this doesn’t work (Incorrect syntax near ‘quotename’). Is the answer wrong?
I know I can generate the string first, put it in a variable then use it with exec sp_executeSql, but I would rather do it the way in the question if it can work…
Best solution I could come up with for you is to pre-quote the value: