Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3696262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:43:04+00:00 2026-05-19T04:43:04+00:00

I am looking to insert records from xml into two different tables. For example

  • 0

I am looking to insert records from xml into two different tables.
For example

<Root>
    <A>
        <AValue>value</AValue>
        <Children>
            <B>
                <BValue>2</BValue>
            </B>
        </Children>
    </A>
    <A>
        <AValue>value</AValue>
        <Children>
            <B>
                <BValue>3</BValue>
            </B>
        </Children>
    </A>
</Root>

Would insert a record into table A
Assuming the identity is starting at 1

AID  AValue
1    value
2    value

also insert a record into table B

BID AID  BValue
1   1    2
2   2    3

I have this

DECLARE @idoc INT
DECLARE @doc NVARCHAR(MAX)

SET @doc = '
<Root>
    <A>
        <AValue>value</AValue>
        <Children>
            <B>
                <BValue>2</BValue>
            </B>
        </Children>
    </A>
     <A>
        <AValue>value</AValue>
        <Children>
            <B>
                <BValue>3</BValue>
            </B>
        </Children>
    </A>
</Root>
'

EXEC sp_xml_preparedocument @idoc OUTPUT, @doc

CREATE TABLE #A
    (
      AID INT IDENTITY(1, 1) ,
      AValue varchar(100)
    )

INSERT  INTO #A
        SELECT  *
        FROM    OPENXML (@idoc, '/Root/A',2)
                WITH (AValue varchar(100)
                      )  

CREATE TABLE #B
    (
      BID INT IDENTITY(1, 1) ,
      AID INT ,
      BValue INT
    )

INSERT  INTO #B
        SELECT  *
        FROM    OPENXML (@idoc, '/Root/A/Children/B',2)
                WITH (
                AID INT,
                BValue INT
                      )

SELECT  *
FROM    #A
SELECT  *
FROM    #B

DROP TABLE #A
DROP TABLE #B
exec sp_xml_removedocument @idoc

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:43:05+00:00Added an answer on May 19, 2026 at 4:43 am

    You need to maintain correlation, which the below shows you how to do. I also made it more robust by adding quite a few bits of flexibility.

    DECLARE @A table
        (
    -- start with non-1, to show the solution is not dependent on starting at 1
          AID INT IDENTITY(14, 1) ,
          AValue varchar(100)
        )
    DECLARE @B TABLE
        (
          BID INT IDENTITY(1, 1) ,
          AID INT ,
          BValue INT
        )
    
    DECLARE @xml XML
    
    -- allow for duplicate values on A.AValue
    -- allow for multiple B nodes
    -- allow for A nodes without B children
    SET @xml = '
    <Root>
        <A>
            <AValue>value2</AValue>
            <Children>
                <B>
                    <BValue>2</BValue>
                </B>
                <B>
                    <BValue>4</BValue>
                </B>
            </Children>
        </A>
         <A>
            <AValue>value1</AValue>
            <Children>
                <B>
                    <BValue>3</BValue>
                </B>
            </Children>
        </A>
         <A>
            <AValue>value1</AValue>
            <Children>
                <B>
                    <BValue>9</BValue>
                </B>
            </Children>
        </A>
         <A>
            <AValue>valueX</AValue>
        </A>
    </Root>
    '
    
    -- dump the data into a temp table for correlating A and B entries
    -- since an A can have multiple B children, ARow identifies which A it really is,
    --    multiple A records can have the same AValue
    
    declare @tmp table (ARow int, avalue varchar(100), bvalue int)
    INSERT @tmp (ARow, avalue, bvalue)
    SELECT X.N, X.C.value('A[1]/AValue[1]','varchar(100)'), Y.V
    FROM (
        SELECT T.C.query('.') C, row_number() over (order by C) N
        FROM @xml.nodes('//A') T(C)) X
    OUTER APPLY (
        SELECT T2.C2.value('.','int') V
        FROM X.C.nodes('A[1]/Children/B/BValue') T2(C2)) Y
    
    -- uncomment next line to see what has gone into @tmp
    -- select * from @tmp
    
    insert @A
    select AValue
    from (
        select distinct ARow, AValue
        from @tmp) X
    order by ARow -- order by is important to maintain correlation
    
    -- get the last identity generated to correlate AID in B
    declare @lastid int
    SET @lastid = scope_identity()
    
    -- Max(ARow) is how many A records were entered, add back ARow to get
    -- the ID generated for the A record
    
    insert @B (AID, BValue)
    select @lastid-M.M+ARow, BValue
    from @tmp, (select max(ARow) M from @tmp) M
    where BValue is not null
    order by ARow
    
    -- check results
    select * from @A
    select * from @B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to insert data from a PHP form in to an XML
I am looking the fastest way to insert many records at once (+1000) to
I'm looking for a way to insert a new entry into a specific row
I need to load Dimensions from EDW Tables (which does maintain historical records) and
I am looping through several hundred records from a XML file and inserting in
I am looking to insert and update records in a database using functions and
currently I'm using an OLE DB Source (SQL Server) to insert records into a
I have a direct load insert of 54,061,487 records. I'm looking for speed. I
Recently I have been looking into Cassandra from our new project's perspective and learned
I am inserting records through a query similar to this one: insert into tbl_xyz

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.