Link to home
Start Free TrialLog in
Avatar of meperera
meperera

asked on

MS SQL Increase the primary key by 15000

Hi experts,
  I am combining two MS SQL databases. The primary key of the of the main table of the databases is CustNumber. Since both tables have numbers starting from 1. I am thinking about incrementing the number of the second table by 15000. (Example, First database will have numbers starting from 1 and the second from 15001). How can I achieve this ? Is there an SQL command that can do this ?
Avatar of aesmike
aesmike

dbcc checkident (<your table>, RESEED, <new seed>)
Avatar of meperera

ASKER

thanks aesmike,
  could you please explain this process ?
Avatar of DBAduck - Ben Miller
Well, you would want to insert into the table that is becoming the main one with the numbers incremented.

SET IDENTITY_INSERT parenttable ON

INSERT INTO parenttable (CustNumber, otherfield, otherfield2)
SELECT CustNumber+15000, otherfield, otherfield2
FROM secondarytable

SET IDENTITY_INSERT parenttable OFF
it is not 100% clear what you are trying to do:

* update the existing records in the second table?
 -> you need to insert the records into the same table with the IDENTITY_INSERT as shown, and then delete the original records.
 NOTE: if you have foreign keys pointing to that table, you will need to update those values in between the insert and the delete!

* select from both tables, but make the PK values from the second tables only +15000
 -> just use a UNION query, with the query from the second table with PK+15000

* copy over the rows from the second table to the first table
 -> dbaduck's code is what you need.
ASKER CERTIFIED SOLUTION
Avatar of aesmike
aesmike

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Meperera,

Everyone is taking for granted that the column you mentioned is an identity column. Check that as well, if not all answers given do not apply. Check first !
I have to admit that it sure looks like it, but you'd better be safe than sorry.