I have 2 tables: Tags and TagsTemp.
Tags has millions of rows while TagsTemp has thousands.
They both are physical tables.
The Primary Key in both tables is ID and it is Auto ID (and so IDs are in different numbers, they do not match).
I have to move all data from TagsTemp to Tags.
That is simple. I did this:
BEGIN TRANS
INSERT INTO Tags (C1, C2, C3)
SELECT C1, C2, C3 FROM TagsTemp
DELETE FROM TagsTemp
COMMIT
The problem is that TagsTemp could have new records during this transaction (during the time between the insert and the delete statements).
I do not want to delete table with records that were not inserted.
I was thinking of something like:
BEGIN TRANS
INSERT INTO @T (ID, C1, C2, C3)
SELECT ID, C1, C2, C3 FROM TagsTemp
INSERT INTO Tags (C1, C2, C3)
SELECT C1, C2, C3 FROM @T
DELETE FROM TagsTemp
WHERE ID IN (SELECT ID FROM @T)
COMMIT
But this is seems not the best way and bad performance.
Is there any better solution to do that?
Note: I cannot change Tables structure.