0

There is a table which has a varchar identifier. There are also several SP which generate this number by getting max and incrementing by 1.

How Can I Lock the table so that I could reserve ID's for e.g. 50 new records, and none user in the meantime hasn't inserted record in my "range" I have obtained.

Is it possible something like this?

3
  • 5
    W H Y is the "sequence" number a VARCHAR ?!?!?!? Commented Jan 23, 2012 at 12:32
  • because It is about 20 characters formatted value (alphanumeric value) Commented Jan 23, 2012 at 12:34
  • 1
    @John - Insert a dummy record with currentmax + 50 and perform the rest of your work after that. A better solution might be to get the next value from a seperate table where you an increment at will but that would require you to change all your stored procedures. Commented Jan 23, 2012 at 12:40

2 Answers 2

2

This is an incredibly difficult problem to solve, which is why every RDBMS implementation has a built in auto-incrementing identifier data type.

USE AN IDENTITY FIELD

That is really the only valid solution to this. If you have a high transaction rate, there will be an incredible amount of locking and blocking in your code to perform something that the engine is designed to handle automatically for you.

If you need an alphanumeric field, there's no reason you can't have an identity field and a varchar and use them as a dual key.

Sign up to request clarification or add additional context in comments.

Comments

0

Do the insert inserts in a transaction.

Example:

BEGIN TRANSACTION MyIdentifierRange
     EXEC usp_createNewIdentifier 
COMMIT TRANSACTION MyIdentifierRange

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.