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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.