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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:02:35+00:00 2026-05-25T16:02:35+00:00

(using SS2008) Within a sql server database, I’d like to copy all the data

  • 0

(using SS2008) Within a sql server database, I’d like to copy all the data of one client for a new client. In other words, generate exact duplicates of all records that pertain to client #1, except now the client ID field of the new records refer to client #2. This is all inside the same database.

Normally I would do this with a series of INSERT commands on the relevant tables selecting the client #1 records. However, some of the tables have autonumber ID columns, and these IDs are referenced as foreign keys in child tables. So when generating the child table records, I’d need to know and refer to the newly created autonumber Ids.

What is the cleanest way to go about this? Can it be done with SQL Server replication? My knowledge of SQL Server is pretty moderate.

  • 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-25T16:02:35+00:00Added an answer on May 25, 2026 at 4:02 pm

    I would do something like this:

    -- Set up a placeholder for the new id
    DECLARE @NewID INT;
    
    -- INSERT parent record
    INSERT INTO myTable (field1, field2)
    SELECT field1, field2 FROM myTable WHERE ID = 1
    
    -- Get the new ID
    SET @NewID = (SELECT SCOPE_IDENTITY());
    
    -- Insert child records with new id 
    INSERT INTO OtherTable (fkid, field1, field2)
    SELECT @NewID, field1, field2 FROM OtherTable WHERE ID = 1
    

    Now if we need to deal with thousands of records, this could work:

    -- Add a new column in the database to manage where the record came from
    ALTER TABLE myTable ADD ParentID int NULL
    
    -- INSERT parent record
    INSERT INTO myTable (field1, field2, ParentID)
    SELECT 
        field1
        , field2
        , ID 
    FROM myTable 
    WHERE SomeCondition IS True
    
    -- Insert child records with new id 
    INSERT INTO OtherTable (fkid, field1, field2)
    SELECT 
        myTable.ID
        , OtherTable.field1
        , OtherTable.field2 
    FROM 
        OtherTable 
        INNER JOIN myTable ON OtherTable.FKID = myTable.ParentID
    
    -- Once unneeded, drop the temporary column
    -- ALTER TABLE myTable DROP COLUMN ParentID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using VS 2010 Premium RTMRel (10.0.30319.1) and SS2008. I have a database
Using SQL Server 2005 Leave Table ID StartDate EndDate 001 02/03/2010 02/03/2010 002 02/03/2010
using Core Data, how would I list (i.e. return an NSArray of NSStrings) all
Using SQL Server 2000 I Want to Check the colum value whether it is
Using C# and System.Data.SqlClient, is there a way to retrieve a list of parameters
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using JQuery, how can I work out if one of the a tags in
Using jQuery, I can find table row in the following ways. Which one is
I am trying to create a calculated field in one of my database tables,
Using Core Data. Let's say we have models for Team and Player. Assume: -Each

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.