I have a table with INT Id
column. I need to select Ids as comma separated values in a subquery.
I tried:
DECLARE @ids VARCHAR(MAX)
SELECT @ids= COALESCE(@ids +',' ,'') + Convert(nvarchar(8), Id)
FROM TableX
SELECT @ids
It works but the problem is that I cannot use declared variable. Is there a way to avoid variable use?
Example:
SELECT Id FROM TableX
Result:
1
2
5
7
I want:
1,2,5,7