I'm attempting to merge multiple SQL Server databases into one. The problem is that one of the tables has an identity field as the primary key. The table also has two uniqueid fields. Something like:
datatable:
id pk identity
link1 uniqueid
link2 uniqueid
My initial thought was to run the following script:
declare @maxId as int
set @maxId = ( SELECT IsNull(MAX(id),0) FROM datatable)
SET IDENTITY_INSERT datatable ON
INSERT INTO database1.datatable id, link1, link2 SELECT id + @maxId, link1, link2 FROM database2.datatable
SET IDENTITY_INSERT datatable OFF
Which works, except that is the query is run multiple times the data gets duplicated. What would be the best way to make sure a combination of link1 and link2 do not already exist in my table?
If it was only one column I could use the IN statement, but unfortunately I need both colums to match.