I have this Stuff expression:
select stuff((SELECT ','''+ITEMID+'''' FROM INVENTTABLE WHERE WK_ITEMSOLVED = 1 and ITEMTYPE = 1 for xml path(''),type).value('.', 'nvarchar(max)'), 1, 1, '')
that returns: (every Item is wrapped in quotes)
Now I need to place this expression inside dynamical SQL. But I can't manage to code those quotes that embed the ITEMID correctly.
declare @sql nvarchar(max) set @sql = 'select stuff((SELECT '',''+ITEMID+'''' from INVENTTABLE WHERE WK_ITEMSOLVED = 1 and ITEMTYPE = 1 for xml path(''''),type).value(''.'', ''nvarchar(max)''), 1, 1, '''')' exec sp_executesql @sql
This is not working - nor does any other attempt. The error it throws alters between 'unclosed quotes' and 'invalid column name' according to the amount of quotes I place