select cd from loc
This will return one row per record.
DECLARE @something NVARCHAR(10) DECLARE @together NVARCHAR(max) = '' DECLARE @delimiter NVARCHAR(5) = ', ' DECLARE Alist CURSOR FOR SELECT cd FROM loc -- <<<< put your query here. OPEN Alist FETCH NEXT FROM Alist INTO @something WHILE @@FETCH_STATUS = 0 BEGIN SET @together = @together + @something FETCH NEXT FROM Alist INTO @something IF @@FETCH_STATUS = 0 SET @together = @together + @delimiter -- Do not add last delimiter if at end of record set. END SELECT @together CLOSE Alist DEALLOCATE Alist
This is a cursor that will save each row into a variable and separate it with commas.