简体   繁体   中英

Inserting XML data into SQL Table with multiple nodes

So I have the following XML:

     <NIACList>
<NIAC>
        <Number></Number>
        <SubmissionDate></SubmissionDate>
        <ExpirationDate  />
        <IssuerIDNO></IssuerIDNO>
        <IssuerName></IssuerName>
        <SuspensionPeriod/>
        <Cessation>
          <Basis  />
          <Date  />
        </Cessation>
        <Merchant>
          <IDNx></IDNx>
          <Name></Name>
          <Address>
            <Region></Region>
            <Locality></Locality>
            <Street></Street>
            <House></House>
            <Block  />
            <Flat  />
            <Phone  />
            <Fax  />
            <Email  />
          </Address>
        </Merchant>
        <CommercialUnit>
          <IDNx  />
          <Name  />
          <Type></Type>
          <Area></Area>
          <Location></Location>
          <Address>
            <Region></Region>
            <Locality></Locality>
            <Street></Street>
            <House></House>
            <Block  />
            <Flat  />
          </Address>
          <Activities>
            <Activity>
              <Code></Code>
              <Name></Name>
            </Activity>
          </Activities>
          <Goods>
            <Good>
              <Name></Name>
            </Good>
          </Goods>
          <WorkProgram  />
          <PublicSupplyUnit>
            <Capacity  />
            <TerraceCapacity  />
          </PublicSupplyUnit>
          <TradingAlcohol  />
          <TradingBeer  />
          <TradingTobaccoProducts  />
          <AmbulatoryTrading  />
          <MobileUnitTrading></MobileUnitTrading>
          <MobileUnit>
            <Type  />
            <Length  />
            <Width  />
            <Height  />
          </MobileUnit>
          <CommercialApparatusTrading></CommercialApparatusTrading>
          <CommercialApparatus>
            <Count  />
            <Length  />
            <Width  />
            <Height  />
          </CommercialApparatus>
        </CommercialUnit>
        <Modifications  />
      </NIAC>
</NIACList>

This is the script for the tables I created:

    create table Merchant (
        IdMerchant int identity  primary key,
        IDNX nvarchar(max) null,
        Name nvarchar(max) null,
        WorkProgram datetime2 null,
        IdAddress int 
        );
    create table Address (
    IdAddress int identity  primary key,
    Region nvarchar(60) null,
    Locality nvarchar(50) null,
    Street nvarchar (60) null,
    House nvarchar (10) null,
    Block nvarchar (10) null,
    Flat nvarchar(10) null,
    Phone nvarchar(30) null,
    Fax nvarchar(60) null,
    Email nvarchar(60) null
    
    );

create table CommercialUnit (
IDCommercialUt int identity primary key,
IDNx nvarchar(90) null,
Name nvarchar(90) null,
Type nvarchar(90) null,
Area int null,
Location nvarchar(50) null,
TerraceCapacity float null,
TradingAlcohol bit null,
TradingBeer bit null,
TradingTobaccoProducts bit null,
AmbulatoryTrading bit null,
MobileUnitTrading bit null,
CommercialApparatusTrading bit null,
IDActivities int ,
IDGoods int ,
IDMobileUnit int ,
IDCommercial int ,
IDPSU int 
);

I'm not very good at XML, but here is the question: I have tables Merchant and Address .The problem is, that the node Address is repeated 2 times(both in Merchant and CommercialUnit nodes), and has different data.My task is to specify somehow the insert, so the data that I want to insert will be divided into 2 categories, one for the Merchant node, and another for the CommercialUnit.After inserting into Address,the records must be linked with the Foreign Key from Merchant and CommercialUnit (IdAddress), so the data will be inserted here also.

I've tried to insert the data, but it inserted from the CommercialUnit node. Below is the code for inserting:

INSERT INTO Address(Region,Locality,Street,House,Block,Flat,Phone,Fax,Email)
        SELECT 
        Region=c.value('Region[1],','nvarchar(60)'),
        Locality=c.value('Locality[1],','nvarchar(50)') ,
        Street=c.value('Street[1],','nvarchar(60)') ,
        House=c.value('House[1],','nvarchar(10)') ,
        Block=c.value('Block[1],','nvarchar(10)') ,
        Flat=c.value('Flat[1],','nvarchar(10)') ,
        Phone=c.value('Phone[1],','nvarchar(30)') ,
        Fax=c.value('Fax[1],','nvarchar(60)') ,
        Email=c.value('Email[1],','nvarchar(60)') 
    FROM @xml.nodes('/NIACList/NIAC/Merchant/Address') Address(c)

You need to do four INSERT statements sequentially:

  1. INSERT INTO Address ... (for Merchant), and capture its newly generated identity value into a variable.
  2. INSERT INTO Merchant ... and use the variable from above.
  3. INSERT INTO Address ... (for CommercialUnit), and capture its newly generated identity value into a variable.
  4. INSERT INTO CommercialUnit ... and use the variable from above.

Conceptual SQL

DECLARE @IdAddress INT;

-- Merchant
INSERT INTO Address ...

-- get last IDENTITY value for the Address table
SET @IdAddress = SCOPE_IDENTITY();

INSERT INTO Merchant ...

-- CommercialUnit
INSERT INTO Address ...

-- get last IDENTITY value for the Address table
SET @IdAddress = SCOPE_IDENTITY();

INSERT INTO CommercialUnit ...

One method is using cross apply . Provided you need distinct addresses only,

SELECT distinct t.*             
    FROM @xml.nodes('/NIACList/NIAC') niac(n)
    cross apply (
      select  Region=c.value('Region[1],','nvarchar(60)'),
        Locality=c.value('Locality[1],','nvarchar(50)') ,
        Street=c.value('Street[1],','nvarchar(60)') ,
        House=c.value('House[1],','nvarchar(10)') ,
        Block=c.value('Block[1],','nvarchar(10)') ,
        Flat=c.value('Flat[1],','nvarchar(10)') ,
        Phone=c.value('Phone[1],','nvarchar(30)') ,
        Fax=c.value('Fax[1],','nvarchar(60)') ,
        Email=c.value('Email[1],','nvarchar(60)') 
        from niac.n.nodes('Merchant/Address') ma(c)
      union
      select  Region=c.value('Region[1],','nvarchar(60)'),
        Locality=c.value('Locality[1],','nvarchar(50)') ,
        Street=c.value('Street[1],','nvarchar(60)') ,
        House=c.value('House[1],','nvarchar(10)') ,
        Block=c.value('Block[1],','nvarchar(10)') ,
        Flat=c.value('Flat[1],','nvarchar(10)') ,
        Phone=c.value('Phone[1],','nvarchar(30)') ,
        Fax=c.value('Fax[1],','nvarchar(60)') ,
        Email=c.value('Email[1],','nvarchar(60)') 
        from niac.n.nodes('CommercialUnit/Address') ua(c)
    ) t 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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