0

I have a column named InvoiceNo, the value for this is generated by code which is based upon the latest invoice number stored in the database and then incremented by one. My problem is the scenario that 2 users at the same time creates an invoice and both sees the same latest invoice number. This would generate the same invoice number for them which is what I'm trying to avoid.

This could be easier if I can set the InvoiceNo column to be unique, but I can't. The column is not unique; it can be edited to an already existing value in the future. It's just that upon creation, we cannot create duplicates.

How can I handle this? I am using SQL Server, EF6.

Dale K
  • 25,246
  • 15
  • 42
  • 71
g_b
  • 11,728
  • 9
  • 43
  • 80
  • 2
    Use a [SQL Server `SEQUENCE`](https://learn.microsoft.com/en-us/sql/t-sql/statements/create-sequence-transact-sql?view=sql-server-ver15) to dish out unique numbers .... – marc_s Mar 30 '21 at 07:54
  • 4
    You need to move the "generated by code" portion. Let the database handle creating the values itself (make `InvoiceNo` an `IDENTITY` or `SEQUENCE`) and your problem goes away. – gvee Mar 30 '21 at 07:54
  • Does this answer your question? [How To Create Table with Identity Column](https://stackoverflow.com/questions/10725705/how-to-create-table-with-identity-column) – Charlieface Mar 30 '21 at 14:37
  • Sorry for the late reply but as I mentioned, I can't make the column unique so it can't be an identity column. – g_b Apr 04 '21 at 14:00

0 Answers0