I have a scenario where I need to generate a batch number (primary key) in the below format.
Batch Number: ( X X ) ( X X X X X ) Location Sequence
Eg: 0100001
0100002
0200001
0100003
0200002
.......
Sequence starts with 00001 for each of the batches. However we cannot have a sequence number generator to do this. The possible solutions that I have over my head are:
- Create an extra table which holds the numbers. But there is a possibility that multiple users get the same sequence as there may be uncommitted transactions.
- Every time an entity is saved we get the max(substring(batchnum,2)) from that column and add +1. But this will have a very huge overload on performance and also has the issue of multiple users getting the same sequence.