在最后一行SQL Server中以逗号分隔值显示记录

时间:2022-01-26 00:20:14

Format

9XSA
ASX9

How can I display the records seperated by comma seperated values in the final row ? using a 'SELECT' query ?

如何在最后一行显示由逗号分隔值分隔的记录?使用'SELECT'查询?

the result should be

结果应该是

Format

9XSA
ASX9,9XSA

if there are three records for instance, then

如果有三个记录,那么

Format

9XSA
7BSF
ASX9,9XSA,7BSF

9XSA 7BSF ASX9,9XSA,7BSF

1 个解决方案

#1


0  

DECLARE @CSV VARCHAR(MAX)
SET @CSV = ''

SELECT @CSV = @CSV + ',' + Field1 FROM Table1

SET @CSV = RIGHT(@CSV, LEN(@CSV)-1)

SELECT Field1 FROM Table1
UNION
SELECT @CSV

#1


0  

DECLARE @CSV VARCHAR(MAX)
SET @CSV = ''

SELECT @CSV = @CSV + ',' + Field1 FROM Table1

SET @CSV = RIGHT(@CSV, LEN(@CSV)-1)

SELECT Field1 FROM Table1
UNION
SELECT @CSV