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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:11:38+00:00 2026-05-10T17:11:38+00:00

Sorry if the title is poorly descriptive, but I can’t do better right now

  • 0

Sorry if the title is poorly descriptive, but I can’t do better right now =(

So, I have this master-detail scheme, with the detail being a tree structure (one to many self relation) with n levels (on SQLServer 2005)

I need to copy a detail structure from one master to the another using a stored procedure, by passing the source master id and the target master id as parameters (the target is new, so it doesn’t has details).

I’m having troubles, and asking for your kind help in finding a way to keep track of parent id’s and inserting the children without using cursors or nasty things like that…

table model for the problem

This is a sample model, of course, and what I’m trying to do is to copy the detail structure from one master to other. In fact, I’m creating a new master using an existing one as template.

  • 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-10T17:11:38+00:00Added an answer on May 10, 2026 at 5:11 pm

    If I understand the problem, this might be what you want:

    INSERT dbo.Master VALUES (@NewMaster_ID, @NewDescription)  INSERT dbo.Detail (parent_id, master_id, [name]) SELECT detail_ID, @NewMaster_ID, [name] FROM dbo.Detail  WHERE master_id = @OldMaster_ID  UPDATE NewChild SET parent_id = NewParent.detail_id FROM dbo.Detail NewChild JOIN dbo.Detail OldChild ON NewChild.parent_id = OldChild.detail_id JOIN dbo.Detail NewParent ON NewParent.parent_id = OldChild.parent_ID WHERE NewChild.master_id = @NewMaster_ID AND   NewParent.master_id = @NewMaster_ID AND   OldChild.master_id = @OldMaster_ID 

    The trick is to use the old detail_id as the new parent_id in the initial insert. Then join back to the old set of rows using this relationship, and update the new parent_id values.

    I assumed that detail_id is an IDENTITY value. If you assign them yourself, you’ll need to provide details, but there’s a similar solution.

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

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 58k
  • 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
  • added an answer Option 1: Write a function similar to this and place… May 11, 2026 at 8:44 am
  • added an answer getattr(f, field), if I understand you correctly (that is, if… May 11, 2026 at 8:44 am
  • added an answer Get rid of the extern and you're set. This code… May 11, 2026 at 8:44 am

Top Members

Trending Tags

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

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.