I want to be able to increment, return and update the next available order number from a 'pot' held in a control table. So one possible approach would be:
But what if someone else is executing the same logic simultaneously? How do I guarantee that their UPDATE has not been actioned before my SELECT? If that happens I could be returned a number which is one greater than the one I need.
I assume that I'd have to implement some kind of locking mechanism but I'm not sure how to go about this.
I appreciate that I could use an IDENTITY column with @@IDENTITY to address this problem but I'd prefer to have control over the numbering and I expect that the same problem arises in other scenarios where an IDENTITY column is not appropriate.
Code:
UPDATE RefStore SET NextOrderNo=NextOrderNo+1
SELECT NextOrderNo FROM RefStore
I assume that I'd have to implement some kind of locking mechanism but I'm not sure how to go about this.
I appreciate that I could use an IDENTITY column with @@IDENTITY to address this problem but I'd prefer to have control over the numbering and I expect that the same problem arises in other scenarios where an IDENTITY column is not appropriate.