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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:35:12+00:00 2026-05-11T10:35:12+00:00

This follows on from this question: Modify XML in SQL server to add a

  • 0

This follows on from this question:
Modify XML in SQL server to add a root node

In addition to the problem posed and now fixed in that question, I am also splitting the existing table into 2, where the 2nd table is a sub-table of the first, with a foreign key back to the first.

So currently

table1(ID, col1, col2, col3....coln) 

Is becoming

table1(ID, col1, col2, col3) 

and

table2(PK, FK_table1, col4, col5, ...coln) 

At the moment I haven’t removed anything from the existing table, just created the second, and I have discovered the OUTPUT construct so that I can insert into both tables at once, including putting the PK of the 1st table into the 2nd as FK in the 1 insert – so something like;

INSERT INTO table1 col1, col2,...etc...  OUTPUT inserted.ID, col1, col2, .... INTO table2(FK_table1, col1, col2, .... )  Select  col.value('node1[1]', 'int') col1,  col.value('node2[1]', 'varchar(50)') col2, ....etc...... FROM @xml.nodes('//Items/Item') doc(col) 

This successfully inserts into both tables at once BUT relies on the fact I haven’t removed the columns from table1 yet, **AND* results in a 1:1 relationship between the tables, when what I need is for table1 to just have rows for distinct col1, col2, col3 , with table2 holding the more detailed info

I’m wondering if I am going about this the right way? Am I going to be able to do this in 1 query or am I going to have to break it down? I’m thinking maybe I could shred the xml first into table1, then shred again separately into table2, joining on table1 to get the FK for the insert? Anyone done this sort of thing before?

  • 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. 2026-05-11T10:35:12+00:00Added an answer on May 11, 2026 at 10:35 am

    Yes, I’m done similar and used both suggestions depending on what other work I need to do

    Options:

    1. Shred the XML once into a tabe variable SELECT DISTINCT from that into table1, SELECT into table2

    2. Shred the XML twice.

    I’d be inclined to do the expensive operation (shredding XML) first in one go.

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

Sidebar

Ask A Question

Stats

  • Questions 87k
  • Answers 87k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since you have curly braces in the string you need… May 11, 2026 at 5:27 pm
  • Editorial Team
    Editorial Team added an answer The whole approach feels wrong headed to me. If your… May 11, 2026 at 5:27 pm
  • Editorial Team
    Editorial Team added an answer Take a look into the file 'format' in your repository-path.… May 11, 2026 at 5:27 pm

Related Questions

I have some predicates being dynamically built that have the following signature passes through
I have a question on the best way of exposing an asynchronous remote interface.
Note The question below was asked in 2008 about some code from 2003. As
While cross-site scripting is generally regarded as negative, I've run into several situations where

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.