I have almost 10 token machines where customers are getting token numbers from table Token. I am using a stored procedure for accessing this table and updating it
Id Name TokenFrom TokenTo LastUsedToken
----------------------------------------------------
1 Token 551 999 562
I have notices that during rush hours a lot of customers are getting duplicate numbers. Maybe this problem is showing up because 10 customers are getting tokens at the same time/hour/second.
Here is issuedToken table
Id PosId Barcode Date Status Number isTaken
1 2 6282019214317 2016-10-20 09:41:45.020 1 366 1
2 2 6282019215918 2016-10-20 09:42:15.020 1 367 1
3 2 6282019225016 2016-10-20 09:42:45.020 1 368 1
4 3 6282019230812 2016-10-20 09:42:55.020 1 369 1
Even sometimes same number is coming on two cashier machines also. I am getting and updating Next Token Number on POS using this Update statement
UPDATE POS
SET tNo = (SELECT TOP 1 NUMBER
FROM Tickets
WHERE STATUS = 1
AND isTaken = 1
AND PosId = (SELECT CGROUP
FROM POS
WHERE NAME='ABC'))
WHERE NAME = 'ABC'
I have 3-3 POS in one group that's why selecting cGroup and in table it's PosId.
I have asked question related to this question before in this question, where someone help me to write a stored procedure for accessing Token Number easily.
But still I am having duplication issue. Can anyone tell me what the best way to avoid duplication ?
sequenceor anidentity(insert a record then usescope_identity). Any other convoluted MAX or manual locking method will just give you problems.