SQL Stored Procedure with multiple access

I have an SQL stored procedure to get a pointer value.  The stored procedure is initiated by a VB6 program.  The SP gets the value of a field (SubPtr), increments the field value, tests it to see if it is equal to PtrMax.  If equal then it sets the field value to 0.  The returned value is used by the VB6 program.  The SP works fine.  My question is what happens in a multi-user environment where there may be many users accessing the SP potentially at the same time?  What I need to have happen is that no two (or more) users can get the same return value.  The table (mytable) is a table which only has a single entry (row).
      'SQL Transaction code to create the stored procedure
      '( @sptr int OUTPUT )
      ' DECLARE @err INT
      ' SELECT @sptr = COALESCE(SubPtr, 0)+1 FROM mytable WITH (UPDLOCK, HOLDLOCK)
      ' SET @err = @@ERROR
      ' IF @err = 0
      ' BEGIN
      '   UPDATE mytable SET SubPtr = CASE WHEN SubPtr = PTRMax THEN 0 Else SubPtr+1 END
      '   SET @err = @@ERROR
      ' End
      ' IF @err = 0
      ' BEGIN
      '   Commit
      ' End
      ' Else
      ' BEGIN
      '   set @sptr = NULL
      '   Rollback
      ' End

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vadim RappCommented:
It can be made simpler:

  begin transaction
  update mytable set SubPtr=case when subptr+1>ptrmax then 0 else subptr+1 end
  select @sptr=subptr from mytable

Open in new window

This is how you can verify that it works correctly in multiuser: insert waitfor delay '00:00:10' between update and select; open 2 query windows, so each is using its own connection; run s.p. from the 1st window, and while it's waiting for the delay, run it also from the 2nd window. You will see that 2nd query will wait for the 1st one, and will return different result.

Practically speaking, with a table having only one row and trivial stored procedure like this one, the chances of different users to interfere are zero.

I dropped error-checkingcode because this code can't result in an error (the only possibility is if you set prtmax to the maximum integer value - so don't do that). Equally, no need in COALESCE - rather than write special function to handle the situation that can happen only once in a lifetime, simply make sure to fill the column once you have created the table.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TopCatOnyxAuthor Commented:
Good idea with the test.  Also, I like the rewritten SP.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.