Reputation: 553
I have a XML like below
<MYLIST>
<Frame Id="89900981" Name="Gxown" Qty="0.0">
<Frame Id="354653-01" Name="Frametop" Qty="1.0" />
<Frame Id="656997-23" Name="FrameNM" Qty="1.0" />
<Frame Id="776879921" Name="PINOBK" Qty="2.0" />
</Frame>
</MYLIST>
Now I want to load the data into table like below
+----------+-----------+----------+-----+
| HEAD | SUBS | NAME | QTY |
+----------+-----------+----------+-----+
| 89900981 | 354653-01 | Frametop | 1.0 |
+----------+-----------+----------+-----+
| 89900981 | 656997-23 | FrameNM | 1.0 |
+----------+-----------+----------+-----+
| 89900981 | 776879921 | PINOBK | 2.0 |
+----------+-----------+----------+-----+
Basically under the MYLIST
node, the first node Frame
is the Head of all. So I want to put that as in HEAD
column (same for all rows under it) and other in SUBS
column. Is this possible? I have tried using the output MYLIST_Frame_Frame
. With this i'm able to load the SUBS
,NAME
and QTY
column but the HEAD is available in MYLIST_Frame
. I'm not sure how to combine this. I have tried using MERGE
but that is inserting HEAD as a separate row.
Upvotes: 0
Views: 50
Reputation: 11
you can use this code in your package: The core part of the answer is about using openxml function. It has three parameter in which the second one is in xpath format.
declare @Xml nvarchar(4000) = '
<MYLIST>
<Frame Id="89900981" Name="Gxown" Qty="0.0">
<Frame Id="354653-01" Name="Frametop" Qty="1.0" />
<Frame Id="656997-23" Name="FrameNM" Qty="1.0" />
<Frame Id="776879921" Name="PINOBK" Qty="2.0" />
</Frame>
'
declare @idoc INT
EXEC sp_xml_preparedocument @idoc OUTPUT, @Xml;
select
(select
Id as Head
from openxml(@idoc, '/MYLIST/Frame', 1)
with (Id nvarchar(32))) as Head,
Id as SUBS,
[Name],
Qty
from openxml(@idoc, '/MYLIST/Frame/Frame[position()<=3]', 1)
with
(Id nvarchar(32),
[Name] nvarchar(32),
[Qty] decimal(2,1))
Upvotes: 1