將數據表的某一列值,轉換爲逗號分隔字符串:ide
先準備一些數據:spa
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代碼的事:code
DECLARE @CommaDelimitedString NVARCHAR(MAX) SELECT @CommaDelimitedString = ISNULL(@CommaDelimitedString + ',', '') + [Datas] FROM @t ORDER BY [Datas] SELECT @CommaDelimitedString