I have a table with a several unique numbers per product type that I want to use to autogenerate the ID for a second table. Every time the unique number is used, I will increment it. But I want to avoid users reading the same unique number so that user1 is able to create the ID and unique number incremented - at the same time user2 read the previous unique number and won't be able to create an ID in the second table. My requirements were to use some type of record lock -- Do I need some type of record lock on the unique number? Or can you suggest a design alternative for me?
-> tableUniqueNumbers:
productType uniqueNumber
widgets 40
gadgets 25
-> tableProducts
productID productName
w38 widgetX
w39 widgetY
g23 gadgetA
g24 gadgetB
user1:
- reads uniqueNumber from tableUniqueNumbers
- uniqueNumber is used to create a record in tableProducts
- uniqueNumber is incremented
If user2 reads the uniqueNumber prior to uniqueNumber being incremented - their new record in tableProducts would try to be created with a duplicate id.
READPAST hint for not updating session - due to avoid beeing blocked by update
if you will use Rowlock ( that can be blocker
more:
http://msdn.microsoft.com/en-us/library/ms187373.aspx
http://www.developerfusion.co.uk/show/1688/4/