列值转换为逗号分隔字符串
将数据表的某一列值,转换为逗号分隔字符串:
先准备一些数据:
DECLARE @t AS TABLE([Datas] NVARCHAR(40)) INSERT INTO @t ([Datas]) VALUES(N'DF'),(N'W4F'),(N'EYY'),(N'ER'),(N'GFF'),(N'A445') SELECT [Datas] FROM @t ORDER BY [Datas]
一二句SQL代码的事:
DECLARE @CommaDelimitedString NVARCHAR(MAX) SELECT @CommaDelimitedString = ISNULL(@CommaDelimitedString + ',', '') + [Datas] FROM @t ORDER BY [Datas] SELECT @CommaDelimitedString