E
E
Eugene2018-07-11 12:15:10
SQL Server
Eugene, 2018-07-11 12:15:10

How to implement sequential numeric ID generation in MS SQL?

I stumbled upon the problem of sudden Identity Seed jumps: when the database server is restarted, the value can increase by 1000. Which ultimately leads to the presence of something like this in the table: records are numbered from 1, 2, 3 ... then 1001, 1002 and so on.
I read what's what; apparently, you should not build your logic around Identity. How to implement a consistent increment with integrity support, if this is a business need? Only the option with the addition of one more field comes to mind ... the logic of which needs to be just written.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
awesomer, 2018-07-11
@evgshk

because this mechanism is to guarantee uniqueness, not to guarantee non-breaking sequence. everything is fine.
ordering business logic for such things is not correct.
if you need sequential number generation is really important - SELECT MAX

N
Nikita, 2018-07-11
@Smiz001

Why not switch to uniqueidentifier?

K
Konstantin Tsvetkov, 2018-07-11
@tsklab

value can increase by 1000
Either you have replication on this database, or failover is configured incorrectly (without rolling back uncommitted transactions).
with the addition of one more field ... the logic of which needs to be just written
Yes, in the trigger, either calculate the missing number, or create a separate table of unused numbers and take from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question