我希望你能幫助我。我正在使用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查詢嗎? –
是的,當然@keyur: – AgusmanPriantoro
我在我的帖子中添加了我的查詢。 @keyur – AgusmanPriantoro