I’m aware of IDENTITY fields but I have a feeling that I couldn’t use one to solve my problem.
Let’s say I have multiple clients. Each client has multiple orders. Each client needs to have their orders numbered sequentially, specific to them.
Example table structure:
Orders: OrderID | ClientID | ClientOrderID | etc...
Some example rows for this table would be:
OrderID | ClientID | ClientOrderID | etc... 1 | 1 | 1 | ... 2 | 1 | 2 | ... 3 | 2 | 1 | ... 4 | 3 | 1 | ... 5 | 1 | 3 | ... 6 | 2 | 2 | ...
I know the naive way would be to take the MAX ClientOrderID for any client and use that value for INSERTs but that would be subject to concurrency issues. I was considering using a transaction but I’m not quite sure what the broadest isolation scope that can be used for this. I’ll be using LINQ to SQL but I have feeling that isn’t relevant.
Somebody correct me if I’m wrong, but as long as your MAX() call is in the same step as your insert, you won’t have a problem with concurrency.
So, you could not do
But you can do
I’m assuming OrderID is an identity column.
Again, if I’m incorrect on this, please let me know. Preferably with a URL