列值轉換爲逗號分隔字符串

將數據表的某一列值,轉換爲逗號分隔字符串: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]
Source Code

 

一二句SQL代碼的事:code

 

DECLARE @CommaDelimitedString NVARCHAR(MAX)
SELECT @CommaDelimitedString = ISNULL(@CommaDelimitedString + ',', '') + [Datas] FROM @t ORDER BY [Datas]
SELECT @CommaDelimitedString
Source Code
相關文章
相關標籤/搜索