码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver2008 合并行 拆分成多行

时间:2015-01-12 16:26:18      阅读:548      评论:0      收藏:0      [点我收藏+]

标签:

SELECT vchBoxNumberNew,
data=STUFF((SELECT ‘;‘+vchBloodNo FROM dbo.tbPlasma WHERE vchBoxNumberNew=a.vchBoxNumberNew FOR XML PATH(‘‘)), 1, 1, ‘‘)
FROM dbo.tbPlasma a
where vchBoxNumberNew=‘2014HL000001‘
GROUP BY a.vchBoxNumberNew


with tmpTable as
(
SELECT vchBoxNo,CAST(‘<i>‘ + REPLACE(vchBloodNos, ‘;‘, ‘</i><i>‘) + ‘</i>‘ AS XML) AS KID
FROM dbo.tbBox
where vchBoxNo=‘2014HL000001‘
)
select vchBoxNo,x.i.value(‘.‘, ‘VARCHAR(10)‘) AS vchBloodNo
from tmpTable
CROSS APPLY KID.nodes(‘i‘) x(i)
where len(x.i.value(‘.‘, ‘VARCHAR(10)‘))<>‘‘

sqlserver2008 合并行 拆分成多行

标签:

原文地址:http://www.cnblogs.com/settingsun/p/4218425.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!