当前位置: 数据库>sqlserver
sql 行转列示例代码
来源: 互联网 发布时间:2014-08-29
本文导语: sql语句实现将一列数据转化为字符串的代码。 代码示例: SELECT DISTINCT u1.[ItemCode] , REPLACE(( SELECT u2.[DetailDescription] + ',' AS 'data()' FROM [YinTaiContent].[dbo].ItemProfileDetails u2 ...
sql语句实现将一列数据转化为字符串的代码。
代码示例:
SELECT DISTINCT
u1.[ItemCode] ,
REPLACE(( SELECT u2.[DetailDescription] + ',' AS 'data()'
FROM [YinTaiContent].[dbo].ItemProfileDetails u2
WHERE u2.[ItemCode] = u1.[ItemCode]
FOR
XML PATH('')
) + '$', ',$', '') AS Roles
FROM [dbo].ItemProfileDetails u1
WHERE u1.ItemCode = '09-001-0039'
u1.[ItemCode] ,
REPLACE(( SELECT u2.[DetailDescription] + ',' AS 'data()'
FROM [YinTaiContent].[dbo].ItemProfileDetails u2
WHERE u2.[ItemCode] = u1.[ItemCode]
FOR
XML PATH('')
) + '$', ',$', '') AS Roles
FROM [dbo].ItemProfileDetails u1
WHERE u1.ItemCode = '09-001-0039'