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

  • Home
  • SEARCH
  • 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 7988735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:32:40+00:00 2026-06-04T12:32:40+00:00

I have three tables table1, table2, table3 with col1, col2 and identity ID column.

  • 0

I have three tables table1, table2, table3 with col1, col2 and identity ID column. These table relationship are defined in the database.

I am trying to create a stored procedure that accepts xml string input and save that data into tables.

This is the XML input

<root>
 <table1 col1='a' col2='b'>
  <table2Array>
   <table2 col1='c' col2='d'>
    <table3array>
     <table3 col1='g' col2='h' />
     <table3 col1='i' col2='j' />
    </table3array>
   </table2>
  <table2 col1='c' col2='d'>
   <table3array>
    <table3 col1='k' col2='l' />
    <table3 col1='i' col2='j' />
   </table3array>
  </table2>
 </table2Array>
</table1>
 <table1 col1='a' col2='b'>
  <table2Array>
   <table2 col1='e' col2='f'>
    <table3array>
     <table3 col1='i' col2='j' />
     <table3 col1='i' col2='j' />
    </table3array>
   </table2>
   <table2 col1='e' col2='f'>
    <table3array>
     <table3 col1='g' col2='h' />
     <table3 col1='g' col2='h' />
    </table3array>
   </table2>
  </table2Array>
 </table1>
</root>

This xml is coming from a third party object and we don’t have control to modify the third party object to emit a different format xml.

Algorithm:

  1. Loop through each node
  2. Insert node attribute into table
  3. Get last identity value
  4. Call child nodes with last identity value as foreign key
  5. Do until no more child nodes

Is this the only way to handle this situation? If so how to iterate through xml node?

Please help!!

Thanks,

Esen

  • 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-06-04T12:32:42+00:00Added an answer on June 4, 2026 at 12:32 pm

    Using merge and output you can do this without a loop using a technique described here.

    I assume your table structure is like this.

    create table Table1
    (
      Table1ID int identity primary key,
      Col1 char(1),
      Col2 char(1)
    )
    
    create table Table2
    (
      Table2ID int identity primary key,
      Table1ID int references Table1(Table1ID),
      Col1 char(1),
      Col2 char(1)
    )
    
    create table Table3
    (
      Table3ID int identity primary key,
      Table2ID int references Table2(Table2ID),
      Col1 char(1),
      Col2 char(1)
    )
    

    The code

    declare @T1 table (XMLCol xml, TargetID int);
    declare @T2 table (XMLCol xml, TargetID int);
    
    merge Table1 as T
    using (select T1.XMLCol.query('*'),
                  T1.XMLCol.value('@col1', 'char(1)'),
                  T1.XMLCol.value('@col2', 'char(1)')
           from @XML.nodes('/root/table1') as T1(XMLCol)) as S(XMLCol, Col1, Col2)
    on 1 = 0
    when not matched then
      insert (Col1, Col2) values (S.Col1, S.Col2)
    output S.XMLCol, inserted.Table1ID into @T1;          
    
    merge Table2 as T
    using (select T2.XMLCol.query('*'),
                  T1.TargetID,
                  T2.XMLCol.value('@col1', 'char(1)'),
                  T2.XMLCol.value('@col2', 'char(1)')
           from @T1 as T1  
             cross apply T1.XMLCol.nodes('table2Array/table2') as T2(XMLCol)) as S(XMLCol, ID1, Col1, Col2)
    on 1 = 0
    when not matched then
      insert (Table1ID, Col1, Col2) values (S.ID1, S.Col1, S.Col2)
    output S.XMLCol, inserted.Table2ID into @T2;          
    
    insert into Table3(Table2ID, Col1, Col2)
    select T2.TargetID,
           T3.XMLCol.value('@col1', 'char(1)'),
           T3.XMLCol.value('@col2', 'char(2)') 
    from @T2 as T2
      cross apply T2.XMLCol.nodes('table3array/table3') as T3(XMLCol);
    

    SE-Data (select "Text-only results" to see all resultsets)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have two tables. Table1 have two columns, col1 and col2, where col1
So I have a table with three columns - col1 , col2 and col3
I have a SQL Server 2008 database table with three varchar Columns - Col1,
Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
I have three tables. The designs were like student table create table student (studID
I have a database with three tables: user_table country_table city_table I want to write
I have three tables like this: Person table: person_id | name | dob --------------------------------
I have two very large enterprise tables in an Oracle 10g database. One table
I have a select statement returning 5 columns: select col1,col2,col3,col4,col5 from table1; col1 col2
I have a query like queryStr := 'SELECT col1, col2, col3, col4 FROM Table1

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.