可能重複:
SQL Server: Can I Comma Delimit Multiple Rows Into One Column?獲取升序級聯排
腳本: -
CREATE TABLE Category (
ID int,
ErrorMessage nvarchar(200)
)
INSERT INTO Category
(ID, ErrorMessage)
VALUES
(1, 'efgh')
INSERT INTO Category
(ID, ErrorMessage)
VALUES
(1, 'abcd')
INSERT INTO Category
(ID, ErrorMessage)
VALUES
(1, 'ijkl')
INSERT INTO Category
(ID, ErrorMessage)
VALUES
(2, 'efgh')
INSERT INTO Category
(ID, ErrorMessage)
VALUES
(2, 'abcd')
INSERT INTO Category
(ID, ErrorMessage)
VALUES
(2, 'ijkl')
預計輸出
Id ErrorMessage
----------------------------------
1 abcd,efgh,ijkl
2 abcd,efgh,ijkl
請注意,類別ID是分組的,用逗號分隔的消息按升序排列。
什麼版本的SQL Server? – 2011-04-19 05:06:20
Sql server 2005 – 2011-04-19 05:06:44