首页 > 代码库 > sqlserver2008 合并行 拆分成多行

sqlserver2008 合并行 拆分成多行

SELECT vchBoxNumberNew,
data=http://www.mamicode.com/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 合并行 拆分成多行