Avatar of bfuchs
bfuchs
Flag 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?
Microsoft SQL Server 2005Microsoft AccessDatabases

Avatar of undefined
Last Comment
Kelvin Sparks

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kelvin Sparks

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
PatHartman

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

ASKER
It worked, thank you!
Your help has saved me hundreds of hours of internet surfing.
fblack61
Kelvin Sparks

You have to set on and off, as only one table at any one time can have this setting.