stuff(
(select ','+ --指定符号
Test --拼接列
from TableName --表名
where --条件
for xml path('')
),1,1,''
)
示例
DECLARE @Temp TABLE
(id INT IDENTITY (1,1),
TestName NVARCHAR(50),
CreateDate DATETIME
)
INSERT @Temp
(
TestName,
CreateDate
)
VALUES
( N'张三',
'2022-06-06 16:53:09.753'
),
( N'张三',
'2022-06-07 14:00:00.000'
),
( N'李四',
'2022-06-06 16:53:09.753'
),
( N'李四',
'2022-06-01 14:00:00.000'
),
( N'李四',
'2022-06-04 14:00:00.000'
),
( N'李四',
'2022-06-05 14:00:00.000'
);
select stuff(
(select ','+ TestName
from @Temp where TestName='李四'
for xml path('')
),1,1,''
)
--输出结果:李四,李四,李四,李四