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 8585087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:58:27+00:00 2026-06-11T21:58:27+00:00

How to parse nested xml in sql server into single table. Considering RowGuid is

  • 0

How to parse nested xml in sql server into single table. Considering RowGuid is unique for each Customer

eg

I want to parse this xml in a single table which would be denormalized and will contain one to many relationships. Considering every nesting has business primary key.

<Customers>
    <Customer>
         <Type xsi:nil="true" />
          <RowGuid>FEFF32BC-1DAB-4F8A-80F0-CFE293C0BEC4</RowGuid>
          <AccountId>0</AccountId>
          <AccountNumber>bdb8eb51-d</AccountNumber>
          <AccountTransactions>
            <AccountTransaction>
                <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                <Balance>500</Balance>
            </AccountTransaction>
            <AccountTransaction>
                <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                <Balance>500</Balance>
            </AccountTransaction>
         </AccountTransactions>
        <Addresses>
             <Address>
                    <city>DELHI</city>
             </Address>
             <Address>
                    <city>MUMBAI</city>
             </Address>
         </Addresses>
      </Customer>
    <Customer>
         <Type xsi:nil="true" />
          <RowGuid>C3D4772E-1DAB-4F8A-80F0-CFE293C0BEC4</RowGuid>
          <AccountId>0</AccountId>
          <AccountNumber>bdb8eb51-d</AccountNumber>
          <AccountTransactions>
            <AccountTransaction>
                <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                <Balance>500</Balance>
            </AccountTransaction>
            <AccountTransaction>
                <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                <Balance>500</Balance>
            </AccountTransaction>
         </AccountTransactions>
      </Customer>

  • 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-11T21:58:28+00:00Added an answer on June 11, 2026 at 9:58 pm

    If table doesn’t need to be normalized you can do LEFT JOIN. I also added a namespace to the Customers element, it is needed because of xsi:nil="true". Try it:

    DECLARE @xml XML =
    '<Customers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Customer>
             <Type xsi:nil="true" />
              <RowGuid>FEFF32BC-1DAB-4F8A-80F0-CFE293C0BEC4</RowGuid>
              <AccountId>0</AccountId>
              <AccountNumber>bdb8eb51-d</AccountNumber>
              <AccountTransactions>
                <AccountTransaction>
                    <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                    <Balance>500</Balance>
                </AccountTransaction>
                <AccountTransaction>
                    <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                    <Balance>500</Balance>
                </AccountTransaction>
             </AccountTransactions>
            <Addresses>
                 <Address>
                        <city>DELHI</city>
                 </Address>
                 <Address>
                        <city>MUMBAI</city>
                 </Address>
             </Addresses>
          </Customer>
        <Customer>
             <Type xsi:nil="true" />
              <RowGuid>C3D4772E-1DAB-4F8A-80F0-CFE293C0BEC4</RowGuid>
              <AccountId>0</AccountId>
              <AccountNumber>bdb8eb51-d</AccountNumber>
              <AccountTransactions>
                <AccountTransaction>
                    <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                    <Balance>500</Balance>
                </AccountTransaction>
                <AccountTransaction>
                    <PaymentDate>2012-09-13 22:19:58</PaymentDate>
                    <Balance>500</Balance>
                </AccountTransaction>
             </AccountTransactions>
          </Customer>
    </Customers>'
    
    SELECT  a.[Type],
            a.RowGuid,
            a.AccountId,
            a.AccountNumber,
            b.PaymentDate,
            b.Balance,
            c.[Address]
    FROM    
    (
        SELECT  
                Customer.value('Type[1]', 'VARCHAR(500)') [Type],
                Customer.value('RowGuid[1]', 'UNIQUEIDENTIFIER') RowGuid,
                Customer.value('AccountId[1]', 'INT') AccountId,
                Customer.value('AccountNumber[1]', 'VARCHAR(500)') AccountNumber
        FROM    @xml.nodes('/Customers/Customer') tbl(Customer)
    ) a
    LEFT JOIN
    (
        SELECT  
                AccountTransaction.value('PaymentDate[1]', 'DATETIME') PaymentDate,
                AccountTransaction.value('Balance[1]', 'DECIMAL(20, 2)') Balance,
                AccountTransaction.value('../../RowGuid[1]', 'UNIQUEIDENTIFIER') RowGuid
        FROM    @xml.nodes('/Customers/Customer/AccountTransactions/AccountTransaction') tbl(AccountTransaction)
    )   b ON
        a.RowGuid = b.RowGuid
    LEFT JOIN
    (
        SELECT  
                Address.value('city[1]', 'VARCHAR(500)') [Address],
                Address.value('../../RowGuid[1]', 'UNIQUEIDENTIFIER') RowGuid
        FROM    @xml.nodes('/Customers/Customer/Addresses/Address') tbl(Address)        
    )   c ON
        a.RowGuid = c.RowGuid
    

    UPDATE:

    Due to the large query cost of the first version of this query (the one that uses XML data type methods) I created another version that uses OPENXML instead of nodes and value methods. There’s a great difference in cost in favor of OPENXML approach:

    DECLARE @handle INT
    
    CREATE TABLE #Customer (Type VARCHAR(500),
        RowGuid UNIQUEIDENTIFIER,
        AccountId INT,
        AccountNumber VARCHAR(500)
    )
    
    CREATE TABLE #AccountTransaction (
        PaymentDate DATETIME,
        Balance DECIMAL(20, 2),
        RowGuid UNIQUEIDENTIFIER
    )
    
    CREATE TABLE #Address (
        City VARCHAR(500),
        RowGuid UNIQUEIDENTIFIER
    )
    
    EXEC sp_xml_preparedocument @handle OUTPUT, @xml
    
    INSERT  #Customer
    SELECT  *
    FROM    OPENXML(@handle, '/Customers/Customer', 2)
    WITH    (
            Type VARCHAR(500),
            RowGuid UNIQUEIDENTIFIER,
            AccountId INT,
            AccountNumber VARCHAR(500)
    )
    
    INSERT  #AccountTransaction
    SELECT  *
    FROM    OPENXML(@handle, '/Customers/Customer/AccountTransactions/AccountTransaction', 2)
    WITH    (
            PaymentDate DATETIME,
            Balance DECIMAL(20, 2),
            RowGuid UNIQUEIDENTIFIER '../../RowGuid[1]'
    )
    
    INSERT  #Address
    SELECT  *
    FROM    OPENXML(@handle, '/Customers/Customer/Addresses/Address', 2)
    WITH    (
            city VARCHAR(500),
            RowGuid UNIQUEIDENTIFIER '../../RowGuid[1]'
    )
    
    SELECT  a.*,
            b.PaymentDate,
            b.Balance,
            c.City
    FROM    #Customer a
    LEFT    JOIN #AccountTransaction b ON
            b.RowGuid = a.RowGuid
    LEFT    JOIN #Address c ON
            c.RowGuid = a.RowGuid
    
    EXEC sp_xml_removedocument @handle
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse something like this: Hi [{tagname:content}] [{tag1:xnkudfdhkfujhkdjki diidfo now nested tag
I am trying to parse this xml file, which has some nested nodes. I
I want write a little code analyzer which parses nested structures and translates into
I want to pass parsed_param to nested select. Like this . select name ,ord.sq,ord.sp
I parse a big xml document with Sax, I want to stop parsing the
I parse an xml file containing books, for each new node I go: Book
I want to use RestKit to parse XML data from our servers. Downloading and
I've got this data I get from an XML feed and parse as NSDictionaries.
I'm using Perl's XML::Simple to parse deeply nested XML and would like to extract
All I want to do is parse the XML below and delete Peter and

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.