patternsqlMinor
SQL Server - Copy data from one database to another - identical tables, new identity values needed
Viewed 0 times
tablesidentityvaluesnewneededsqlidenticaldatabaseoneanother
Problem
Here's the thing, I have been running identical copies of my website for two different geographical regions. The site now supports multi-currency pricing so I want to merge both sites together.
All tables that I need to merge are Identity based and foolishly the matching tables in each copy of the database were seeded at the same number.
Is there a way to copy the data from one database to another at the same time giving the copy data new identity values that also are reflected in the foreign key values of referenced tables?
i.e.
And so on..
All tables that I need to merge are Identity based and foolishly the matching tables in each copy of the database were seeded at the same number.
Is there a way to copy the data from one database to another at the same time giving the copy data new identity values that also are reflected in the foreign key values of referenced tables?
i.e.
Account
- Id
- Email
- FirstName
- etc
AccountImage
- Id
- Account_id
- FileName
- etcAnd so on..
Solution
Unless I'm missing something your problem is not moving the data it's dealing with the identity values that are already set up. If that is the case then try this.
As long as you are consistent in the value you are adding all of your relationships will stay the same into the combined DB. So for example
You have an employee table with a current max id of 200,000 in DB A and 1,400,000 in DB B. You decide to move the data from DB A to DB B because it means moving less data. As you move your employee table you add 2,000,000 to the Employee_Id column.
And last piece of advice back up everything before you start in case you make a mistake :)
- Pick a value greater than your current ident values on either DB. I would pick a round value, say 1,000,000.
- Pick the ident values you want to change (for example if you have lookup tables that are the same for both DBs then you probably want to leave them the same.)
- When you move your data add the value you selected in step 1 to the idents value you want to change as you move them.
As long as you are consistent in the value you are adding all of your relationships will stay the same into the combined DB. So for example
You have an employee table with a current max id of 200,000 in DB A and 1,400,000 in DB B. You decide to move the data from DB A to DB B because it means moving less data. As you move your employee table you add 2,000,000 to the Employee_Id column.
USE DatabaseB
GO
SET IDENTITY_INSERT Employee ON
INSERT INTO Employee (Employee_Id, Other_Columns)
SELECT Employee_Id + 2000000, Other_Columns
FROM DatabaseA.dbo.Employee
SET IDENTITY_INSERT Employee OFF
GOAnd last piece of advice back up everything before you start in case you make a mistake :)
Code Snippets
USE DatabaseB
GO
SET IDENTITY_INSERT Employee ON
INSERT INTO Employee (Employee_Id, Other_Columns)
SELECT Employee_Id + 2000000, Other_Columns
FROM DatabaseA.dbo.Employee
SET IDENTITY_INSERT Employee OFF
GOContext
StackExchange Database Administrators Q#57855, answer score: 8
Revisions (0)
No revisions yet.