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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:57:21+00:00 2026-06-18T12:57:21+00:00

The Situation: I am inserting information from one table to another, a source and

  • 0

The Situation:
I am inserting information from one table to another, a source and target. When the information is inserted into the target, a primary key is created. (In this case it is an integer.) I then need to be able to tie back to the source table. However, based on the data being moved, I am not able to reliably get the 1:1 match between the target and source tables.

The Question:
Is there a way to copy the primary key that was created for record(x) in the target table and copy it as a foreign key to that same record(x) in the source table as the bulk insert is happening?

Details:
I am trying to get this done in SQL. I have a work-around to this problem but I figure there has to be a way to do what I’m asking.

  • 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-18T12:57:23+00:00Added an answer on June 18, 2026 at 12:57 pm

    I found my answer after reading this great article.

    http://sqlblog.com/blogs/adam_machanic/archive/2009/08/24/dr-output-or-how-i-learned-to-stop-worrying-and-love-the-merge.aspx

    I acheived what I was looking for by using a MERGE and its OUTPUT clause. Here is my sample code that I used to figure this out.

    I started by creating 3 temporary tables, #Temp2, #Temp3 and #Temp4. #Temp2 is considered the source table. #Temp3 would be the target table and #Temp4 is a bridge. I then inserted a few rows of very simple data, in this case just one field – Value.

    CREATE TABLE #Temp2(
    OldID INT IDENTITY(1,1),
    Value INT,
    NewFK INT)
    
    CREATE TABLE #Temp3(
    NewerID INT IDENTITY(1,1),
    Value INT)
    
    CREATE TABLE #Temp4(
    OldID INT NOT NULL,
    NewerID INT NOT NULL,
    Value INT)
    
    INSERT INTO #Temp2(Value)
    VALUES(30), (40), (50), (70)
    
    INSERT INTO #Temp3(Value)
    VALUES (333), (444), (555), (777)
    

    Then comes the MERGE statement that does the dirty work. It will be taking the value from #Temp2 and putting it into #Temp3. It will then take the ID created in #Temp3, the ID from #Temp2 and the Value that was passed, and throw them all into #Temp4.

    MERGE INTO #Temp3 AS tgt
    USING #Temp2 AS src
    ON 1=0
    WHEN NOT MATCHED THEN
        INSERT(
        Value)
        VALUES(
        src.Value)
    OUTPUT
        src.OldID,
        INSERTED.NewerID,
        src.Value
    INTO #Temp4(OldID, NewerID, Value);
    

    Then I ran an UPDATE to the staging table #Temp2 to update the NewFK field with the new ID. Lastly, do a simple SELECT to see the updated information.

    UPDATE X
    SET X.NewFK = Z.NewerID
    FROM #Temp2 X
    JOIN #Temp4 Z
    ON X.OldID = Z.OldID
    
    SELECT * FROM #Temp2
    

    This acheived exactly what I needed and is a pretty streamlined way of doing things. I hope this will help some people who come across this question. Thanks everyone for your insight and responses.

    NOTE:
    I believe MERGE was introduced in SQL Server 2008.

    Jonathan

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

Sidebar

Related Questions

I am inserting records from one table to other where the status is lead.
I have a situation where I want to associate multiple values from one table
I have a situation with IE and a table that I am inserting from
Situation: I often run into this problematic and never know how to solve it.
Consider the following tables: CREATE TABLE user_roles( pkey SERIAL PRIMARY KEY, bit_id BIGINT NOT
I'm inserting an input field into the dom via jquery in a situation that
On my table I have a secondary unique key labeled md5. Before inserting, I
I've got a classic case of UPDATE or INSERTing some data into a table.
The situation is something like this: PostgreSQL Database table has a field dateAdded which
Here is my basic situation. I'm trying to use NHibernate to get information from

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.