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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:05:17+00:00 2026-06-08T22:05:17+00:00

I’ve never had to post a question on StackOverflow before because I can always

  • 0

I’ve never had to post a question on StackOverflow before because I can always find an answer here by just searching. Only this time, I think I’ve got a real stumper….


I’m writing code that automates the process of moving data from one SQL Server database to another. I have some pretty standard SQL Server Databases with foreign key relationships between some of their tables. Straight forward stuff. One of my requirements is that the entire table needs to be copied in one fell swoop, without looping through rows or using a cursor. Another requirement is I have to do this in SQL, no SSIS or other external helpers.

For example:

INSERT INTO TargetDatabase.dbo.MasterTable 
SELECT * FROM SourceDatabase.dbo.MasterTable

That’s easy enough. Then, once the data from the MasterTable has been moved, I move the data of the child table.

INSERT INTO TargetDatabase.dbo.ChildTable 
SELECT * FROM SourceDatabase.dbo.ChildTable

Of course, in reality I use more explicit SQL… like I specifically name all the fields and things like that, but this is just a simplified version. Anyway, so far everything’s going alright, except …

The problem is that the primary key of the master table is defined as an identity field. So, when I insert into the MasterTable, the primary key for the new table gets calculated by the database. So to deal with that, I tried using the OUTPUT INTO statement to get the updated values into a Temp table:

INSERT INTO TargetDatabase.dbo.MasterTable 
OUPUT INSERTED.* INTO @MyTempTable
SELECT * FROM SourceDatabase.dbo.MasterTable

So here’s where it all falls apart. Since the database changed the primary key, how on earth do I figure out which record in the temp table matches up with the original record in the source table?

Do you see the problem? I know what the new ID is, I just don’t know how to match it with the original record reliably. The SQL server lets me output the INSERTED values, but doesn’t let me output the FROM TABLE values along side the INSERTED values. I’ve tried it with triggers, I’ve tried it with an SP, always I have the same problem.

If I were just updating one record at a time, I could easily match up my INSERTED values with the original record I was trying to insert to see the old and new primary key values, but I have this requirement to do it in a batch.

Any Ideas?

PS: I’m not allowed to change the table structure of the target or source table.

  • 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-08T22:05:20+00:00Added an answer on June 8, 2026 at 10:05 pm

    You can use MERGE.

    declare @Source table (SourceID int identity(1,2), SourceName varchar(50))
    declare @Target table (TargetID int identity(2,2), TargetName varchar(50))
    
    insert into @Source values ('Row 1'), ('Row 2')
    
    merge @Target as T
    using @Source as S
    on 0=1
    when not matched then
      insert (TargetName) values (SourceName)
    output inserted.TargetID, S.SourceID;
    

    Result:

    TargetID    SourceID
    ----------- -----------
    2           1
    4           3
    

    Covered in this blog post by Adam Machanic: Dr. OUTPUT or: How I Learned to Stop Worrying and Love the MERGE

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.