Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am doing sqlbulkcopy from one table to another table.
There are so many tables to do that.
I am copying data with for loop for each tables.

But, i am facing with an issue, whenever i copy data from source table to destination table than identity column value is getting changed.

Suppoose there is value like this in identity column of a source table
301
305
605

but when i copy data than it will imported in destination table like that
1
2
3

1) I have tried,
set identity_insert [tablename] ON
//COPY Data from source to destination
set identity_insert [tablename] OFF

2) I have also set SqlBulkCopyOption.KeepIdentity

Any solution will be highly appriciable.
Posted
Updated 1-Nov-14 0:56am
v2
Comments
OriginalGriff 1-Nov-14 7:21am    
Don't "Bump" your question: it's rude and it doesn't help you get an answer. Quite the reverse in fact.
By all means improve your question to help us answer it, but just editing it to move it to the top of the queue is rude.
Mehdi Gholam 1-Nov-14 7:23am    
You can't unless you overwrite the destination data.
Maciej Los 1-Nov-14 13:29pm    
Does SELECT statement contains ID field?
Thava Rajan 4-Nov-14 2:29am    
query please

You can bulkinsert using KEEPIDENTITY and then reseed the Identity if necessary.

More info on MSDN.[^]
 
Share this answer
 
Hi Nirav
Try not to include the identity column in your source table,
Also when you are inserting into the destination table, do not include the identity column
 
Share this answer
 
Can the identity column datatype in the destination table accomodate the identity column data type from the source table. for example if the identity column for the the destination table is tinyint then it will not will be able to accomodate some int values from the source table hence the regeneration.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900