copy data from one database to another with different primary key

Hi.  

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 a.id = b.id. 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!
lbjamisonAsked:
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
SET IDENTITY_INSERT dbo.tbl1 ON 

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 = b.id
WHERE not exists (select 1 from dbo.tbl1 where id m ic.to_id)

SET IDENTITY_INSERT dbo.tbl1 OFF
GO

Open in new window

0
 
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
0
 
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.
0
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.

 
RimvisCommented:
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
0
 
jogosCommented:
To be able to insert an identity-column
SET IDENTITY_INSERT dbo.yourtable ON
http://msdn.microsoft.com/en-us/library/ms188059.aspx
0
 
ben9Commented:
Hi,

Reading the question, you did

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

This cannot be done if the two tables have the same structure and if a.id 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 a.id = b.id

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.
0
 
ben9Commented:
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 a.id = b.id

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 a.id = b.id part)

INSERT INTO db1.dbo.tbl1 SELECT *  FROM db2.dbo.tbl2
0
 
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.
0
 
jogosCommented:
then please accept the solution(s) that helped you
0
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.