copy data from one database to another with different primary key


I was supposed to copy data from one database to another. I used INSERT INTO db1.dbo.tbl1 a SELECT * FROM db2.dbo.tbl2 b WHERE = This statement works fine.

Suppose that tbl1 and tbl2 are two identical tables.  My problem is, I need to insert the same data to the destination table (tbl1) but this time with a different new id. The data is still coming from the source table (tbl2). How would I do this? Do I still have to use that INSERT INTO SELECT statement? And how would I include that NEW ID to the destination table?

Please help!
Who is Participating?
jogosConnect With a Mentor Commented:
In addition to the SET IDENTITY_INSERT-comment.

When you got the from_id/to_id somewhere in a table you can join it in your select
using db1

INSERT INTO dbo.tbl1 a (id, col1, col2, ...)
SELECT (ic.to_id, b.col1, b.col2, ...)
FROM db2.dbo.tbl2 b 
INNER JOIN db2.dbo.IdConversion  as ic on ic.from_id =
WHERE not exists (select 1 from dbo.tbl1 where id m ic.to_id)


Open in new window

Pratima PharandeCommented:
you mean to say id's are autoincremented for bothe the tables
if yes then you can try giving column names , do not use id it will get poupulated automatically as autoincremented

INSERT INTO db1.dbo.tbl1 ( col1,col2,col3)
 SELECT col1,col2,col3 FROM db2.dbo.tbl2
lbjamisonAuthor Commented:
IDs were supposed to be as parameters (from_id and to_id). There would be a lot of tables that are involve with the same ID parameter to use. I supposed to use dynamic sql for this stored procedure I am making.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Hi lbjamison,

maybe you can insert new records with some offset, for example:

INSERT INTO db1.dbo.tbl1 (record_id, col1,col2,col3)
 SELECT record_id+1000, col1,col2,col3 FROM db2.dbo.tbl2
To be able to insert an identity-column

Reading the question, you did

INSERT INTO db1.dbo.tbl1 a SELECT * FROM db2.dbo.tbl2 b WHERE =

This cannot be done if the two tables have the same structure and if is an identity column. So, were you saying you did this?

INSERT INTO db1.dbo.tbl1 a ({all fields except id})  SELECT{all fields except id} FROM db2.dbo.tbl2 b WHERE =

Also, what is the relationship between the new id and the old id? If the "id" relationship does not exist, you could just insert them with new (server genereated) "id"s by

INSERT INTO db1..tbl1 ({all fields except id}) SELECT {all fields except id} FROM db2.dbo.tbl2

(Note there is no need to exclude id this time)

If there is a specific relationship between the id values of tbl2 and the "new id"s to be added to tbl1, then we need to know about it before we can write any sql.
Oops. I correct my posting.

INSERT INTO db1.dbo.tbl1 a ({all fields except id})  SELECT{all fields except id} FROM db2.dbo.tbl2 b WHERE =

does not make sense.

I confused the case with UPDATE. Obviously you mentioned you did INSERT, so I guess you might have done this? (without where = part)

INSERT INTO db1.dbo.tbl1 SELECT *  FROM db2.dbo.tbl2
lbjamisonAuthor Commented:
Thanks Guys for the help!

I have resolved the problem by using INSERT INTO SELECT and specify the column list of the tables. I think there's no other easy way than that.
then please accept the solution(s) that helped you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.