I have a business requirement that the InvoiceNumber
field in my Invoices
table be totally sequential - no gaps or the auditors might think our accountants are up to something fishy!
My first thought was to simply use the primary key (identity) but if a transaction is rolled back a gap appears in the sequence.
So my second thought is to use a trigger which, at the point of insert, looks for the highest InvoiceNumber
value in the table, adds 1 to it, and uses it as the InvoiceNumber
for the new row. Easy to implement.
Are there potential issues with near-simultaneous inserts? For example, might two near simultaneous inserts running the trigger at the same time get the same 'currently highest InvoiceNumber
' value and therefore insert rows with the same InvoiceNumber
?
Are there other issues I might be missing? Would another approach be better?