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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:06:47+00:00 2026-06-12T23:06:47+00:00

I’ve almost got what I want after shredding up some serious Xml–but after looking

  • 0

I’ve almost got what I want after shredding up some serious Xml–but after looking at the results, I see that in one section of the parsing, I can’t easily resolve this pattern of iterating through all of the line details for each of the subheaders– so instead of writing out a total of let’s say 3 records for all of the line items, I’m writing out three line items for each of the subs–of which let’s say I have two. I wind up with a total of 6! 🙁 I’ve distilled the pattern as a generic header/subheader/detail relationship model in the code that follows.

    DECLARE @MetroXML xml
    SET @MetroXML =
    '<Header>
       <col1>Conoco</col1>
       <col2>ORD-1111</col2>
       <SubHeaders>
         <SubHeader>
          <col1>Dallas</col1>
          <col2>BOL-2213</col2>
         <Details>
          <Detail>
            <col1>Diesel</col1>
            <col2>7600.00</col2>
          </Detail>
         </Details>
        </SubHeader>
        </SubHeaders>
        <SubHeaders>
         <SubHeader>
          <col1>Fort Worth</col1>
          <col2>BOL-2216</col2>
         <Details>
          <Detail>
            <col1>Ethanol</col1>
            <col2>1852.00</col2>
          </Detail>
          <Detail>
            <col1>Unleaded</col1>
            <col2>900.00</col2>
          </Detail>
         </Details>
       </SubHeader>
      </SubHeaders>
     </Header>';


    INSERT INTO [scratch].GenericHeader
     SELECT T.c.value('col1[1]','varchar(10)') AS 'col1',
            T.c.value('col2[1]','varchar(10)') AS 'col2'
       FROM @MetroXML.nodes('/Header') T(c);


    INSERT [scratch].GenericSubHeader
     (id,col1,col2)
    SELECT 
      h.id,
      n.x.value('col1[1]','varchar(10)') AS 'col1',
      n.x.value('col2[1]','varchar(10)') AS 'col2'
     FROM [scratch].GenericHeader h
      CROSS APPLY @MetroXML.nodes('/Header/SubHeaders/SubHeader') n(x);


     INSERT [scratch].GenericDetail
     (id,subid,col1,col2)
      SELECT 
       s.id,
       s.subid,
       n.x.value('col1[1]','varchar(10)') AS 'col1',
       n.x.value('col2[1]','varchar(10)') AS 'col2'
     FROM [scratch].GenericSubHeader s
      CROSS APPLY @MetroXML.nodes('/Header/SubHeaders/SubHeader/Details/Detail') as n(x);


     select * from [scratch].GenericHeader
      where id = 24;

     select * from [scratch].GenericSubHeader
      where id = 24;

     select * from [scratch].GenericDetail
      where id = 24;

NOTE: id,subid,detid are defined as INT IDENTITY(1,1)
Results

What I get:

id|subid|detid|col1     |col2
--------------------------------
24|44   |22   |Diesel   |7600.00
24|44   |23   |Ethanol  |1852.00
24|44   |24   |Unleaded |900.00
24|48   |25   |Diesel   |7600.00
24|48   |26   |Ethanol  |1852.00
24|48   |27   |Unleaded |900.00

What I want to get:

id|subid|detid|col1     |col2
--------------------------------
24|44   |22   |Diesel   |7600.00
24|48   |23   |Ethanol  |1852.00
24|48   |24   |Unleaded |900.00
  • 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-12T23:06:48+00:00Added an answer on June 12, 2026 at 11:06 pm

    The reason you get duplicate rows is because you are using cross apply against the entire XML of each row in GenericSubHeader. You have to figure out a way to map the generated ID in GenericHeader and the generated subid in GenericSubHeader to the related part of the XML .

    If you are on SQL Server 2008 or later you can merge with output to create a table variable that holds the generated id and the xml sub nodes that belongs.

    declare @GH table
    (
      id int,
      sub xml
    );
    
    merge scratch.GenericHeader as T
    using 
      (
        select T.c.value('col1[1]','varchar(10)'),
               T.c.value('col2[1]','varchar(10)'),
               T.c.query('SubHeaders')
        from @MetroXML.nodes('/Header') T(c)
      ) as S(col1, col2, sub)
    on 0 = 1
    when not matched then
      insert (col1, col2) values(S.col1, S.col2)
    output inserted.id, S.sub into @GH;  
    
    declare @GSH table
    (
      id int,
      subid int,
      det xml
    );
    
    merge scratch.GenericSubHeader as T
    using
      (
        select h.id,
               n.x.value('col1[1]','varchar(10)'),
               n.x.value('col2[1]','varchar(10)'),
               n.x.query('Details')
        from @GH as h
          cross apply h.sub.nodes('/SubHeaders/SubHeader') n(x)
      ) as S(id, col1, col2, det)
    on 0 = 1
    when not matched then
      insert (id, col1, col2) values (S.id, S.col1, S.col2)
    output inserted.id, inserted.subid, S.det into @GSH;
    
    insert into scratch.GenericDetail(id, subid, col1, col2)
    select s.id,
           s.subid,
           n.x.value('col1[1]','varchar(10)') AS 'col1',
           n.x.value('col2[1]','varchar(10)') AS 'col2'
    from @GSH as s
      cross apply s.det.nodes('/Details/Detail') as n(x);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites 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.