Trying to create a query that declares a variable that stores a comma-separated int list then re-uses that list in a WHERE statement later on. The parameters will have to be passed in the format IN (int,int,int) etc... and will always change dependent on value of @TG. Below is what I have so far:
DECLARE @TG int = 14168
DECLARE @TG_ITEMS varchar = (SELECT
STUFF((SELECT DISTINCT
',' + CONVERT(varchar(10), i.item_key, 120)
FROM store__tracking_group sitg
JOIN store_tracking_group_detail sit
JOIN item i
WHERE sitg.number IN (@TG)
FOR XML PATH ('')), 1, 1, ''))
--- MAIN QUERY HERE
SELECT ''
FROM ''
WHERE 'xx' IN (@TG_ITEMS)
INoperator expects a list of values separated by commas, while you try to give it a single value containing comma-delimited data. That's the difference betweenIN(1,2,3,4)andIN('1,2,3,4')- the first one will work.