LTZJoe
LTZJoe

Reputation: 86

SQL Server - How to find a records in INSERTED when the database generates a primary key

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.

Upvotes: 1

Views: 1363

Answers (2)

GilM
GilM

Reputation: 3771

To illustrate what I mentioned in the comment:

SET IDENTITY_INSERT TargetDatabase.dbo.MasterTable  ON

INSERT INTO TargetDatabase.dbo.MasterTable  (IdentityColumn, OtherColumn1, OtherColumn2, ...)
SELECT IdentityColumn, OtherColumn1, OtherColumn2, ... 
FROM SourceDatabase.dbo.MasterTable  

SET IDENTITY_INSERT TargetDatabase.dbo.MasterTable  OFF

Okay, since that didn't work for you (pre-existing values in target tables), how about adding a fixed increment (offset) to the id values in both tables (use the current max id value). Assuming the identity column is "id" in both tables:

DECLARE @incr int
BEGIN TRAN
SELECT @incr = max(id)
FROM TargetDatabase.dbo.MasterTable AS m WITH (TABLOCKX, HOLDLOCK)

SET IDENTITY_INSERT TargetDatabase.dbo.MasterTable ON

INSERT INTO TargetDatabase.dbo.MasterTable (id{, othercolumns...})
SELECT id+@incr{, othercolumns...}
FROM SourceDatabase.dbo.MasterTable

SET IDENTITY_INSERT TargetDatabase.dbo.MasterTable OFF

INSERT INTO TargetDatabase.dbo.ChildTable (id{, othercolumns...})
SELECT id+@incr{, othercolumns...} 
FROM SourceDatabase.dbo.ChildTable

COMMIT TRAN

Upvotes: 0

Mikael Eriksson
Mikael Eriksson

Reputation: 138980

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

Upvotes: 3

Related Questions