patternsqlModerate
Guarantee SQL Server Identity Columns that two consecutive values differ by constant increments?
Viewed 0 times
identityguaranteevaluesconstantcolumnssqltwothatdifferserver
Problem
I posed a question about Denali sequences here and another question about emulation of identity columns in Oracle Global Tables here.
I always thought that I could trust that identity values increment by constant increments. On the other side, I know that using sequences I can never be sure that there are not some gaps in them.
Is it time to adapt to gaps in sequences or will it be better to stay with identity columns in temporary tables?
I always thought that I could trust that identity values increment by constant increments. On the other side, I know that using sequences I can never be sure that there are not some gaps in them.
Is it time to adapt to gaps in sequences or will it be better to stay with identity columns in temporary tables?
Solution
You can't guarantee contiguous or consecutive identity values in SQL Server
Also:
You have to adapt to gaps in your numbering, however generated.
- An insert of multiple rows won't guarantee this. See Do Inserted Records Always Receive Contiguous Identity Values.
- The current identity value stays incremented on rollback (either explicit ROLLBACK or implicit due to a CONSTRAINT error).
Also:
- Loading a temp table doesn't guarantee consecutive values. Saying that, this is harder to prove, and works in practice (as many folk rely on it), but be warned...
You have to adapt to gaps in your numbering, however generated.
Context
StackExchange Database Administrators Q#3948, answer score: 14
Revisions (0)
No revisions yet.