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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:33:42+00:00 2026-06-11T01:33:42+00:00

I was given the task of creating a stored procedure to copy every piece

  • 0

I was given the task of creating a stored procedure to copy every piece of data associated with a given ID in our database. This data spans dozens of tables. each table may have dozens of matching rows.

example:

table Account
pk = AccountID

Table AccountSettings
FK = AccountID

Table Users
PK = UserID
FK = AccountID

Table UserContent
PK = UserContentID
FK = UserID

I want to create a copy of everything that is associated with an AccountID(which will traverse nearly every table) The copy will have a new AccountID and UserContentID but will have the same UserID. the new data needs to be in its respective table.
🙂 fun right?

The above is just a sample but I will be doing this for something like 50 or 60 tables.
I have researched using CTEs but am still a bit foggy on them. that may prove to be the best method. MY SQL skills are…… well I have worked with it for about 40 logged hours so far 🙂

Any advice or direction on where to look would be greatly appreciated. In addition, I am not opposed to doing this via C# if that would be possible or better.

Thanks in advance for any help of info.

  • 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-11T01:33:44+00:00Added an answer on June 11, 2026 at 1:33 am

    The simplest way to solve this is the brute force way: write a very long proc that processes each table individually. This will be error-prone and very hard to maintain. But it will have the advantage of not relying on the database or database metadata to be in any particularly consistent state.

    If you want something that works based on metadata, things are more interesting. You have three challenges there:

    1. You need to programmatically identify all the related tables.
    2. You need to generate insert statements for all 50 or 60.
    3. You need to capture generated ids for those tables that are more than one or two steps away from the Account table, so that they can in turn be used as foreign keys in yet more copied records.

    I’ve looked at this problem in the past, and while I can’t offer you a watertight algorithm, I can give you a general heuristic. In other words: this is how I’d approach it.

    1. Using a later version of MS Entity Framework (you said you’d be open to using C#), build a model of the Account table and all the related tables.
    2. Review the heck out of it. If your database is like many, some of the relationships your application(s) assume will, for whatever reason, not have an actual foreign key relationship set up in the database. Create them in your model anyway.
    3. Write a little recursive routine in C# that can take an Account object and traverse all the related tables. Pick a couple of Account instances and have it dump table name and key information to a file. Review that for completeness and plausibility.
    4. Once you are satisfied you have a good model and a good algorithm that picks up everything, it’s time to get cracking on the code. You need to write a more complicated algorithm that can read an Account and recursively clone all the records that reference it. You will probably need reflection in order to do this, but it’s not that hard: all the metadata that you need will be in there, somewhere.
    5. Test your code. Allow plenty of time for debugging.
    6. Use your first algorithm, in step 3, to compare results for completeness and accuracy.

    The advantage of the EF approach: as the database changes, so can your model, and if your code is metadata-based, it ought to be able to adapt.

    The disadvantage: if you have such phenomena as fields that are “really” the same but are different types, or complex three-way relationships that aren’t modeled properly, or embedded CSV lists that you’d need to parse out, this won’t work. It only works if your database is in good shape and is well-modeled. Otherwise you’ll need to resort to brute force.

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

Sidebar

Related Questions

I have been given the task of creating a sql database and creating a
I've recently been given the task of creating an Audit on a database table
I have been given the task to design a database to store a lot
I was given the task of doing a facelift to our current site. I
I was given the task of creating a registration page for an upcoming church
I have been given a new task from the client which is basically creating
I am creating a basic data transfer task using TSQL where I am retrieving
I have been given a task of creating a common Gridview component which has
Hi I've been given the task of creating an N-Teir website using the Entity
I am creating a SQL 2008 R2 stored procedure to duplicate a row 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.