Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys I just have one quick question:

what happens when an insert statement fails on an identity column?

Is it possible that say for example that if I insert a row with an identity column, that identity column will be 1, and insert again but that fails and does not insert and data. Then try to insert again and that identity for that row is now 3?

Any advice will be much appreciated.


1 solution

As per msdn documentation[^], a failed transaction can still cause the number to be incremented.

"Failed statements and transactions can change the current identity for a table and create gaps in the identity column values. The identity value is never rolled back even though the transaction that tried to insert the value into the table is not committed. For example, if an INSERT statement fails because of an IGNORE_DUP_KEY violation, the current identity value for the table is still incremented."

As a result, you always have a chance that some values may be missed.
Share this answer
P.Salini 2-Nov-11 3:35am    
Nice My 5 :-)
Abhinav S 2-Nov-11 3:52am    
Thank you.
Member 8102934 2-Nov-11 4:16am    
Thanks for the help. Much appreciated.
Abhinav S 2-Nov-11 4:58am    
You are welcome.

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