Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

Add values to key field

Hi All,
Just wanted to confirm that is safe to do an append query into an existing table (restoring records mistakenly deleted from a backup database to its original place), and would prefer the ID should get their original value, the ID is defined as PK and has identity property set to yes, identity increment 1.
I know in access if I do that it would mess up the columns AutoNumber and its a kind of a hassle to get rid of all problems that come along, however wondering if SQL is handling it properly?
ASKER CERTIFIED SOLUTION
Avatar of Kelvin Sparks
Kelvin Sparks
Flag of New Zealand 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
I know in access if I do that it would mess up the columns AutoNumber and its a kind of a hassle to get rid of all problems that come along, however wondering if SQL is handling it properly?
Not true.  

Access does allow you to do this and it may work against linked tables also.  I haven't had to restore records in SQL server tables recently.  I would try running the append query from the GUI to see what happens.  However, I do know that if you run the append query from VBA, you do have to follow Kelvin's directions and set the identity insert value before and after.
Avatar of bfuchs

ASKER

@kelvinsparks,
just wonder why do I need set to on/off, what happens if I leave this on all the time?

@PatHartman,
What I meant by access causing trouble is when this would be an access based table back end, not sure about the latest versions but the old versions I still use (2000/2003) definitely don't like when people interfere with their counters..In my experience they would take the last number appended as the next id to generate and would throw an error for users trying to add new record that "It would create duplicate value on Primary key or index..", Sometimes I can get this fixed by adding a higher number and compacting the database, but I had a case where none of this helped..
Avatar of bfuchs

ASKER

It worked, thank you!
You have to set on and off, as only one table at any one time can have this setting.