I would like to retrieve certain users from a full list of a temp table #temptable. The query looks like this:
DECLARE @List varchar(max)
SELECT @List = coalesce(@List + ',','') + '''' + StaffCode + ''''
FROM tblStaffs
SELECT UserName
FROM #temptable
WHERE #temptable.StaffCode IN (@List)
I can tell @List is in a right format:
'AAA','ABB','BBB','CCC','DDD','MMM'
And if I change it to
WHERE #temptable.StaffCode IN ('AAA','ABB','BBB','CCC','DDD','MMM')
It certainly works, then why not IN (@List)?