1 DECLARE @SQL AS VARCHAR(8000)
2SELECT @SQL = ISNULL(@SQL+',','') + ColumnName FROM TableName
3SELECT @SQL
1DECLARE @CarList nvarchar(max);
2SET @CarList = N'';
3SELECT @CarList+=CarName+N','
4FROM dbo.CARS;
5SELECT LEFT(@CarList,LEN(@CarList)-1);
1declare @aa varchar (200)
2set @aa = ''
3
4select @aa =
5 case when @aa = ''
6 then CarName
7 else @aa + coalesce(',' + CarName, '')
8 end
9 from Cars
10
11print @aa