时间:2021-07-01 10:21:17 帮助过:17人阅读
CREATE FUNCTION FN_Merge (@Student NVARCHAR(50))
RETURNS NVARCHAR(50)
AS
BEGIN
DECLARE @Course NVARCHAR(50)
SELECT @Course = ISNULL(@Course + ‘,’,”) + @Course
FROM SC
WHERE Student = @Student
RETURN @COURSE
END
SELECT DISTINCT [Student]
,dbo.FN_Merge([Student]) AS Course
FROM [dbo].[SC]
结果:
(2 row(s) affected)
方法二:FOR XML PATH
SELECT DISTINCT [Student]
,STUFF(
(
SELECT ‘,’+[Course]
FROM [dbo].[SC]
WHERE Student = A.Student
FOR XML PATH(”)
)
,1,1,”
)AS Course
FROM [dbo].[SC] AS A
结果:
(2 row(s) affected)
链接:
T_SQL 将一列多行数据合并为一行
标签:begin info height add www alt object_id zh-cn nvarchar