Halo All,
我希望你能帮助我。我正在使用SQL Server。 我有这样的XML数据:
<Root>
<Header>
<refNo>S350282535-100060</refNo>
<doNumber>0</doNumber>
</Header>
<Header>
<refNo>S350282535-100061</refNo>
<doNumber>1</doNumber>
</Header>
<Detail>
<item>114495</item>
<qty>2</qty>
<uom>PC</uom>
<refNo>S350282535-100060</refNo>
<remarks>-</remarks>
<reason>-</reason>
</Detail>
<Detail>
<item>114496</item>
<qty>2</qty>
<uom>PC</uom>
<refNo>S350282535-100061</refNo>
<remarks>-</remarks>
<reason>-</reason>
</Detail>
<Detail>
<item>114497</item>
<qty>2</qty>
<uom>PC</uom>
<refNo>S350282535-100061</refNo>
<remarks>-</remarks>
<reason>-</reason>
</Detail>
</Root>
我如何获得这种数据:
refNo doNumber Item qty uom
--------------------------------------------------------------------
S350282535-100060 0 114495 2 PC
S350282535-100061 1 114496 2 PC
S350282535-100061 1 114497 2 PC
这是我已经尝试的查询:
DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX), @flagParameter AS
INT
SELECT @XML = XMLData FROM XMLwithOpenXML
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
SET @flagParameter = 2
SELECT item, qty,uom, refNo, remarks, reason
FROM OPENXML(@hDoc, 'Root/Detail',@flagParameter)
WITH
(
item [int],
qty [int],
uom [nvarchar] (6),
refNo [nvarchar] (50),
remarks [nvarchar] (100),
reason [nvarchar] (100)
)
SELECT refNo, doNumber
FROM OPENXML(@hDoc, 'Root/Header',@flagParameter)
WITH
(
refNo [nvarchar](50),
doNumber [int]
)
SELECT refNo , doNumber, item--, qty,uom, remarks, reason
FROM OPENXML(@hDoc, 'Root/Detail',@flagParameter)
WITH
(
refNo [nvarchar] (50),
doNumber [int],
item [int],
qty [int],
uom [nvarchar] (6),
remarks [nvarchar] (100),
reason [nvarchar] (100)
)
UNION
SELECT refNo, doNumber, One as Item--, two as two, three as three, four as
four
FROM OPENXML(@hDoc, 'Root/Header',@flagParameter)
WITH
(
refNo [nvarchar](50),
doNumber [int],
one [int],
two [int],
three [int],
four [int]
) EXEC sp_xml_removedocument @hDoc
我使用UNION,但结果并不像我预期的那样,有人可以帮助我吗?我将非常感谢你的帮助。
答案 0 :(得分:0)
假设XML数据存储在名为@xml
:
DECLARE @xml XML = '<Root>
<Header>
<refNo>S350282535-100060</refNo>
<doNumber>0</doNumber>
</Header>
<Header>
<refNo>S350282535-100061</refNo>
<doNumber>1</doNumber>
</Header>
<Detail>
<item>114495</item>
<qty>2</qty>
<uom>PC</uom>
<refNo>S350282535-100060</refNo>
<remarks>-</remarks>
<reason>-</reason>
</Detail>
<Detail>
<item>114496</item>
<qty>2</qty>
<uom>PC</uom>
<refNo>S350282535-100061</refNo>
<remarks>-</remarks>
<reason>-</reason>
</Detail>
<Detail>
<item>114497</item>
<qty>2</qty>
<uom>PC</uom>
<refNo>S350282535-100061</refNo>
<remarks>-</remarks>
<reason>-</reason>
</Detail>
</Root>'
以下是使用XML methods和XPath / XQuery而不是旧的OPENXML获得预期结果的一种可能方法:
;WITH Header(refNo, doNumber)
AS
(
SELECT
T.C.value('refNo[1]', 'varchar(100)')
,T.C.value('doNumber[1]', 'int')
FROM @xml.nodes('/Root/Header') AS T(C)
)
SELECT
Header.*
,T.C.value('item[1]', 'varchar(100)') 'item'
,T.C.value('qty[1]', 'int') 'qty'
,T.C.value('uom[1]', 'varchar(100)') 'uom'
FROM @xml.nodes('/Root/Detail') AS T(C)
INNER JOIN Header on T.C.value('refNo[1]', 'varchar(100)') = refNo
<强> demo
强>