试图将XML文件导入到主/明细sql表

时间:2019-01-29 18:43:01

标签: sql xml

试图使用SQL存储的Proc。将XML导入表。我希望将MemberInformation部分的email_address,first_name,last_name插入主数据库。然后使用主密钥将实例插入“实例”表中。然后使用主密钥将“事务”插入“交易”表中。随附的示例XML。

    <ArrayOfMemberInformation>
  <MemberInformation Member_Id="111111" Constituent_Id="" Last_Updated="2012-02-01T14:10:57" Last_Updated_By_Constituent_Id="" Last_Updated_By_Name="Jon doe">
    <Column Name="email_address" Value="jdoe@sample.net" />
    <Column Name="first_name" Value="Jon " />
    <Column Name="last_name" Value="Doe" />
    <Column Name="email_address" Value="jdoe@sample.net" />
    <Column Name="first_name" Value="Jon " />
    <Column Name="last_name" Value="Doe" />
    <Instances>
      <Instance Instance_Id="333333-4545-8C4543-6676756">
        <Column Name="ac member" Value="Y" />
         <Column Name="ac gradyear" Value="1833" />
        <Column Name="ac line1" Value="Doe  FAMILY" />
        <Column Name="ac line2" Value="'69  '92    " />
        <Column Name="ac line3" Value="'95  '99" />
        <Column Name="ac line4" Value="" />
        <Column Name="ac line5" Value="" />
         <Column Name="receiptpreference" Value="" />
        <Transactions>
          <Transaction Transaction_Id="11111" IsSuccess="true" IsReviewed="true">
            <Column Name="amount" Value="5.0000" />
             <Column Name="billingname" Value="Jon doe" />
             <Column Name="achchecknum" Value="" />
             <Column Name="billingemail" Value="adDoe7@comcast.net" />
            <Column Name="billingphone" Value="111-222-22222" />
            <Column Name="billingstreet1" Value="44 kel Ct" />
            <Column Name="billingstreet2" Value="" />
            <Column Name="billingcity" Value="city" />
            <Column Name="billingstate" Value="OH" />
            <Column Name="billingzip" Value="55555" />
            <Column Name="billingcountry" Value="US" />

            <TransactionItems>
              <TransactionItem I_Member_Id="222222" Type="Amt" Transaction_Item_Id="Amount-1111">
                <Column Name="total" Value="5.0000" />
              </TransactionItem>
            </TransactionItems>
            <LinkedTransaction>
              <TransactionId>0</TransactionId>
              <Amount>0</Amount>
              <ControlId>0</ControlId>
              <ControlType />
              <ControlName />
              <PaymentType />
              <TotalScheduledAmount>0</TotalScheduledAmount>
            </LinkedTransaction>
            <LedgerValues>
              <LedgerValue Name="ALLCOE " Value="PREP" />
            </LedgerValues>
          </Transaction>
        </Transactions>
      </Instance>
    </Instances>
  </MemberInformation>
</ArrayOfMemberInformation>

1 个答案:

答案 0 :(得分:0)

您的问题离清除 ...

很远

我可以猜到的是,您想从该XML中读取内容。我建议读一下暂存表。然后,您可以继续使用表格数据。您可以添加ref-Id的列,将所有这些都放置在登台表中,然后将数据转移到目标表中。

我不想为您完成所有工作,但是以下内容将从XML中的任何位置读取至少一个值。这将使您有足够的能力自行查找所有其他值。尝试一下,然后再提出一个更具体的问题(新问题)。

    DECLARE @xml XML=
    N'<ArrayOfMemberInformation>
      <MemberInformation Member_Id="111111" Constituent_Id="" Last_Updated="2012-02-01T14:10:57" Last_Updated_By_Constituent_Id="" Last_Updated_By_Name="Jon doe">
        <Column Name="email_address" Value="jdoe@sample.net" />
        <Column Name="first_name" Value="Jon " />
        <Column Name="last_name" Value="Doe" />
        <Column Name="email_address" Value="jdoe@sample.net" />
        <Column Name="first_name" Value="Jon " />
        <Column Name="last_name" Value="Doe" />
        <Instances>
          <Instance Instance_Id="333333-4545-8C4543-6676756">
            <Column Name="ac member" Value="Y" />
            <Column Name="ac gradyear" Value="1833" />
            <Column Name="ac line1" Value="Doe  FAMILY" />
            <Column Name="ac line2" Value="''69  ''92    " />
            <Column Name="ac line3" Value="''95  ''99" />
            <Column Name="ac line4" Value="" />
            <Column Name="ac line5" Value="" />
            <Column Name="receiptpreference" Value="" />
            <Transactions>
              <Transaction Transaction_Id="11111" IsSuccess="true" IsReviewed="true">
                <Column Name="amount" Value="5.0000" />
                <Column Name="billingname" Value="Jon doe" />
                <Column Name="achchecknum" Value="" />
                <Column Name="billingemail" Value="adDoe7@comcast.net" />
                <Column Name="billingphone" Value="111-222-22222" />
                <Column Name="billingstreet1" Value="44 kel Ct" />
                <Column Name="billingstreet2" Value="" />
                <Column Name="billingcity" Value="city" />
                <Column Name="billingstate" Value="OH" />
                <Column Name="billingzip" Value="55555" />
                <Column Name="billingcountry" Value="US" />
                <TransactionItems>
                  <TransactionItem I_Member_Id="222222" Type="Amt" Transaction_Item_Id="Amount-1111">
                    <Column Name="total" Value="5.0000" />
                  </TransactionItem>
                </TransactionItems>
                <LinkedTransaction>
                  <TransactionId>0</TransactionId>
                  <Amount>0</Amount>
                  <ControlId>0</ControlId>
                  <ControlType />
                  <ControlName />
                  <PaymentType />
                  <TotalScheduledAmount>0</TotalScheduledAmount>
                </LinkedTransaction>
                <LedgerValues>
                  <LedgerValue Name="ALLCOE " Value="PREP" />
                </LedgerValues>
              </Transaction>
            </Transactions>
          </Instance>
        </Instances>
      </MemberInformation>
    </ArrayOfMemberInformation>';

-查询会将一些值切入登台表:

SELECT mi.value('@Member_Id','int') Attribute_MemberId --attribute of MemberInformation
      ,mi.value('(Column[@Name="email_address"]/@Value)[1]','nvarchar(max)') PickThecolumnByName_eMail
      ,mi.value('(Column[@Name="first_name"]/@Value)[1]','nvarchar(max)') PickThecolumnByName_FirstName
      ,inst.value('@Instance_Id','nvarchar(max)') Instance_Id
      ,inst.value('(Column[@Name="ac member"]/@Value)[1]','nvarchar(max)') Instance_AcMember
      ,trns.value('@Transaction_Id','nvarchar(max)') Transaction_Id
      ,trns.value('(Column[@Name="amount"]/@Value)[1]','decimal(12,4)') Transaction_Amount
      ,trns.value('(LinkedTransaction/TransactionId)[1]','int') LikedTransactionId
INTO #stagingTable
FROM @xml.nodes('/ArrayOfMemberInformation/MemberInformation') A(mi)
OUTER APPLY A.mi.nodes('Instances/Instance') B(inst)
OUTER APPLY B.inst.nodes('Transactions/Transaction') C(trns);

-检查结果;

SELECT * FROM #stagingTable