Link to home
Start Free TrialLog in
Avatar of johnkainn
johnkainn

asked on

copy from one table to another

I want to copy everything from one table to another.
The tables are identical except that the table that is to be copied to has an identity (incremental) column.
How do I do that?
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

set identity_insert target_table on
insert into target_table
select * from source_table
set identity_insert target_table off
Avatar of johnkainn
johnkainn

ASKER

I have a target table with an Id column that is identity column. The name of the target table is y_B.
If I try to use the code you sent I get following error:

Explicit value must be specified for identity column in table 'y_B' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.


do you want to generate id in target table or get them from the source table?
I want to generate id in target table.
ASKER CERTIFIED SOLUTION
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark Wills
If the only difference between source table and destination table is an additional idenity column in the destination, then you will need to specify the column names...

e.g.  say tbl_destination (id int identity, col1 varchar,col2 int, col3 datetime) and tbl_source (col1 varchar,col2 int, col3 datetime)

then you do...

insert tbl_destination(col1,col2,col3)
select col1,col2,col3
from tbl_source

Thought I had refreshed before posting. Sorry bout that - please see my posting anyway...