views:

27

answers:

3

I have at my test database datatable Tags.

ID int autoincrement
Name nvarchar(255)

I want to transfer this table to my production database.

How to do it if I want to have the same ids foreach element

The problem is that my start index at testdatable is 15, and some ids don't exist because I removed theirs.

So:

Situation at Test datable

15 sport
18 money
30 homework

Desired situation at production datatable

15 sport
18 money
30 homework

At start production datatable is empty, and has autoincrement too.

+4  A: 

Use the identity_insert feature on the destination table before inserting the data into the new table.

SET IDENTITY_INSERT YourNewTable ON

INSERT YourNewTable (ID, Name) SELECT ID,Name FROM YourOldTable

SET IDENTITY_INSERT YourNewTable OFF
JohnFx
Of course you may need a linked server to the dev baox do do this porperly or copy the whole dev table to a staging table onthe prod server and then do this.
HLGEM
thank you very much :)
+1  A: 

You can turn the constraints off for the auto-incrementing field, load your data into the table, and then turn the auto-incrementing field back on.

George
+1  A: 

you can use

SET IDENTITY_INSERT MY_TABLE ON
INSERT MY_TABLE (Id, Field1) VALUES (15, 'sport')
SET IDENTITY_INSERT MY_TABLE OFF

or make a backup/restore if the prod database is empty as you said (assuming it's the same database)

gsharp
Better to use the select clause than the values clause for this.
HLGEM