Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert table A to B and keep the same ID

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2010-05-25 : 09:21:38
I want to insert table A to B and keep the same ID.
ID column in B is Identity = Yes.
Is it possible?

Sachin.Nand

2937 Posts

Posted - 2010-05-25 : 09:30:48
Yes you can do it by setting identity insert on for table B.

PBUH
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-25 : 09:31:08
If the Id not already exists in table b then yes.

set identity_insert table_B on
insert table_b
select * from table_A
set identity_insert table_B off



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2010-05-25 : 09:50:30
The question is that column ID in B is already set Identity = yes and for some reason I can not set it off. (There are 10000 records in B already ID from 1 to 10000)
In A, ID is starting on 15000 and then 15001...20000.
I want to keep B has the same ID as A after inserting.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-25 : 10:03:20
set identity_insert table_B on
means:
let me insert values into the column ID and don't auto generate them.

set identity_insert table_B off
means:
I'm ready with inserting and the table can switch back to auto generate the ID's.

Have you tried my example that I've posted before?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2010-05-25 : 11:12:01
Thank you.
I tried it but have to list all columns to make it working.
Go to Top of Page
   

- Advertisement -