简体   繁体   English

使用 SSIS 将 XML 数据加载到表中

[英]Loading XML data into table using SSIS

I have a XML like below我有一个像下面这样的 XML

<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.基本上在MYLIST节点下,第一个节点Frame是所有的Head。 So I want to put that as in HEAD column (same for all rows under it) and other in SUBS column.所以我想把它放在HEAD列中(它下面的所有行都一样)和其他放在SUBS列中。 Is this possible?这可能吗? I have tried using the output MYLIST_Frame_Frame .我尝试使用输出MYLIST_Frame_Frame With this i'm able to load the SUBS , NAME and QTY column but the HEAD is available in MYLIST_Frame .有了这个,我就可以加载SUBSNAMEQTY列,但 HEAD 在MYLIST_Frame可用。 I'm not sure how to combine this.我不知道如何结合这个。 I have tried using MERGE but that is inserting HEAD as a separate row.我曾尝试使用MERGE但这是将 HEAD 作为单独的行插入。

you can use this code in your package: The core part of the answer is about using openxml function.您可以在您的包中使用此代码:答案的核心部分是关于使用 openxml 函数。 It has three parameter in which the second one is in xpath format.它有三个参数,其中第二个是 xpath 格式。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM