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

The Archive Base Latest Questions

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

Does anyone know a simple way of copying multiple table rows and it’s related

  • 0

Does anyone know a simple way of copying multiple table rows and it’s related items?

In my simplified example I have two tables, one with a table for companies and one for their employees and I’d like to be able to clone entire companies with all their employees in a simple way.

[Companies]

- ID (Auto-increment)
- Name

[Employees]

- ID
- CompanyID (Foreign Key)
- Name

I’ve been playing around with the OUTPUT-clause but just managed to return the inserted IDs without a reference to the original row.

I just can’t figure out how to link a inserted row to the original one?

If the insert-statement returned a table like this:

@mappingTable

-InsertedCompanyID
-OriginalCompanyID

I could run a statement like this to copy all employees:

INSERT INTO Employees (CompanyID, Name)
SELECT m.InsertedCompanyID, x.Name FROM @mappingTable m 
INNER JOIN Employees x (x.CompanyID = m.OriginalCompanyID)

Or am I on the wrong path? Is there’s a better way to accomplish this?

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

    You can use the output clause of the merge statment to map between the old ID and the new auto incremented ID.

    Have a look at this question: Using merge..output to get mapping between source.id and target.id

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

Sidebar

Related Questions

Does anyone know any simple way to retrieve the country from a given IP
Does anyone know of a simple way to compare two strings together to generate
Does anyone know of a simple way to set a selected index or item
Does anyone know of a really simple way of publishing Java methods as web
Does anyone know of a really simple way of capitalizing just the first letter
does anyone know if there is a simple way to bind a textblock to
Does anyone know of any way to convert a simple gif to xaml? E.G.
Does anyone know a simple way of converting a mp4 file to an ogg
does anyone know a simple way to generate a random 64 bit number using
Does anyone know a simple way to check if two polygons, especially rectangles, are

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.