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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:58:01+00:00 2026-05-22T21:58:01+00:00

I want to write script in SQL that will copy these 2 tables(A,B) to

  • 0

I want to write script in SQL that will copy these 2 tables(A,B) to other 2 tables(C,D) with the same structure as A,B accordingly.

IMPORTANT:

  1. Tables C,D are NOT necessary empty
  2. Several processes may call script simultaneously

Table A has foreign key(fk_a_b) of table B

   ________________________  _________________
   |        Table A       |  |   Table B     |  
   |______________________|  |_______________|
   | id     FK_A_B   name |  | id    visible |
   | ----- -------- ------|  | ----- --------|
   | 1      21       n1   |  | 21     true   |
   | 5      32       n2   |  | 32     false  |
   ------------------------  -----------------

Let say that after copying table B to D this is what I get

   ________________
   |   Table D    |  
   |______________|
   | id   visible |
   | ----- -------|
   | 51    true   |
   | 52    false  |
   ----------------

Now, when I’ll copy table A to C I need to know, somehow, that ID=21 maps now to ID=51, and ID=32 to ID=52. Finally, the table C will be:

   ________________________
   |        Table C       |
   |______________________|
   | id     FK_C_D   name |
   | ----- -------- ------|
   | 61      51       n1  |
   | 62      52       n2  |
   ------------------------

Because several processes may call script simultaneously, I CAN’T alter table A,B to add some helper columns. So, to achieve this I used CURSOR. I copied row by row of table B and managed temp table to map OldId to NewId(21->51,32->52) and then used this temp table to copy table A.

I’ve read that CURSOR is bad practice. So, is there another way to do it?

Thank you

  • 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-22T21:58:02+00:00Added an answer on May 22, 2026 at 9:58 pm

    You can use the output clause with the merge statement to get a mapping between source id and target id.
    Described in this question. Using merge..output to get mapping between source.id and target.id

    Here is some code that you can test. I use table variables instead of real tables.

    Setup sample data:

    -- @A and @B is the source tables
    declare @A as table
    (
      id int,
      FK_A_B int,
      name varchar(10)
    )
    
    declare @B as table
    (
      id int,
      visible bit
    )  
    
    -- Sample data in @A and @B
    insert into @B values (21, 1),(32, 0)
    insert into @A values (1, 21, 'n1'),(5, 32, 'n2')
    
    
    -- @C and @D is the target tables with id as identity columns
    declare @C as table
    (
      id int identity,
      FK_C_D int not null,
      name varchar(10)
    )
    
    declare @D as table
    (
      id int identity,
      visible bit
    )  
    
    -- Sample data already in @C and @D
    insert into @D values (1),(0)
    insert into @C values (1, 'x1'),(1, 'x2'),(2, 'x3')
    

    Copy data:

    -- The @IdMap is a table that holds the mapping between
    -- the @B.id and @D.id (@D.id is an identity column)
    declare @IdMap table(TargetID int, SourceID int)
    
    -- Merge from @B to @D.
    merge @D as D             -- Target table
    using @B as B             -- Source table
    on 0=1                    -- 0=1 means that there are no matches for merge
    when not matched then
      insert (visible) values(visible)    -- Insert to @D
    output inserted.id, B.id into @IdMap; -- Capture the newly created inserted.id and
                                          -- map that to the source (@B.id)
    
    -- Add rows to @C from @A with a join to
    -- @IdMap to get the new id for the FK relation
    insert into @C(FK_C_D, name)
    select I.TargetID, A.name 
    from @A as A
      inner join @IdMap as I
        on A.FK_A_B = I.SourceID
    

    Result:

    select *
    from @D as D
      inner join @C as C
        on D.id = C.FK_C_D
    
    id          visible id          FK_C_D      name
    ----------- ------- ----------- ----------- ----------
    1           1       1           1           x1
    1           1       2           1           x2
    2           0       3           2           x3
    3           1       4           3           n1
    4           0       5           4           n2
    

    You can test the code here: https://data.stackexchange.com/stackoverflow/q/101643/using-merge-to-map-source-id-to-target-id

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

Sidebar

Related Questions

I want to write a script that will stop a scheduled task on a
I want to write a sh/bash script that can determine whether a particular directory
I just want a simple tool that will help me quickly write scripts/packages that
I want to write a batch file that will take the contents of a
I need to write a script that disables SQL Server Express 2008 from running.
I want to write a script that retrieve the tweets from a twitter account.
I want to write a bash script that sorts the input by rules in
I want to write a script which cleans the 'run' dialogue automatically every log
I want to write a script in Ruby to clean up some messed up
As a simple example, I want to write a CLI script which can print

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.