How do I lock a record in a MS SQL Table using a stored procedure

I have a VB6 application that uses MS SQL Server to store data.  In the app, I produce work orders each with a unique WO#.  Currently the way I keep track of the next available number is through the use of a table that just has one record and one field called Next_Available_No.

Since there will be potentially dozens of users updating this table at the same time, I want to find a way (using a stored procedure) to read the next available number and increment the table without another user grabbing the number before it has been incremented.

So the question is this - how do I read the next available number and update that table's number by 1 without another user doing the same thing between the time the stored procedure reads the number and updates it?

Thanks for the help.
LVL 7
EYoungAsked:
Who is Participating?
 
TRACEYMARYConnect With a Mentor Commented:
yeah that will work as well...as long as update and return in same program

change
SELECT * FROM SWR_Next_Available_No
to
select SWR_NEXT_Availale_No from SWR_NEXT_Available_No    only need the number returned
0
 
TRACEYMARYCommented:
put table with the following ROWID int in properties set  identity to yes that gives unique records.

the other way i have done it rather than change all my tables to the above is have a separate table.

CREATE TABLE [WEBCONTROL] (
      [ROWID] [int] IDENTITY (1, 1) NOT NULL ,
      [TABLE_NAME] [varchar] (18) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [COLUMN_NAME] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [DESCRIPTION] [varchar] (150) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [ALPHA_PREFIX] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [NEXT_NUMBER] [int] NULL ,
      [ALPHA_SUFFIX] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [LEADING_ZEROS] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
      [DECIMAL_PLACES] [smallint] NULL ,
      [NEXT_NUMBER_FIELD_ID] [int] NULL
) ON [PRIMARY]
GO


then i do this

CREATE PROCEDURE sp_Web_Form_Get_Next_Field_Id

@vcTableName VARCHAR(20),
@vcColumnName VARCHAR(100),
@vcNextFieldId int Output

AS





  BEGIN
     SELECT @vcNextFieldId = NEXT_NUMBER_FIELD_ID
      FROM WEBCONTROL
      WHERE TABLE_NAME = @vcTableName AND COLUMN_NAME = @vcColumnName



UPDATE WEBCONTROL SET NEXT_NUMBER_FIELD_ID = NEXT_NUMBER_FIELD_ID + 1
WHERE TABLE_NAME = @vcTableName AND COLUMN_NAME = @vcColumnName

  END
GO

which gets my next number and updates it to.

then you can use one table to control many numbers.....for any application.

example of data
TABLE_NAME (put in the table that your using for auto numbers i.e wo)
COLUMN_NAME(perhaps wo number)
the other fields are optional........i.e might want to use 0010 for example ...if so have to change sp a little.

then to call it do

exec sp_Web_Form_Get_Next_Field_Id 'WO','WONUMBER'

it will return this @vcNextFieldId int Output which is your next number.
0
 
EYoungAuthor Commented:
Thanks for the suggestion.  I am just trying to control one number in one applicatoin.  Anyway to simplify the above?  Would the following work assuming Table1 contains the next available number?

Create Procedure WO_No AS
    DECLARE @Next_No AS Decimal
    SELECT @Next_No FROM UPDATE Table1 SET Next_Available_No = Next_Available_No


Something like that?

Thanks again for any help
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
EYoungAuthor Commented:
Here, this seems to work:

CREATE Procedure Test

      AS

      DECLARE @Next_No AS Decimal

      UPDATE SWR_Next_Available_No SET SWR_No = SWR_No + 1, @Next_No = SWR_No + 1

      SELECT * FROM SWR_Next_Available_No
GO


Since the update and the get-next-number are in the same UPDATE command, would that be safe enough to assure that no other user could run the procedure at exactly the same time?  Seems like it would work as the UPDATE command must lock the record for the update.  Right?

Thanks
0
 
TRACEYMARYCommented:
yes just use the above....and put one record in the table
TABLE_NAME      WO
COLUMN_NAME   WO_NUM
NEXT_NUMBER_FIELD_ID = 0

then do

exec sp_Web_Form_Get_Next_Field_Id 'WO','WONUMBER'

try it in query run the create and then the exec bit...your get 1....




0
 
EYoungAuthor Commented:
Thanks for helping me think this out.  Whew, glad that is over...
0
 
EYoungAuthor Commented:
One more change.  The actual command should be:

UPDATE SWR_Next_Available_No SET SWR_No = SWR_No + 1, @Next_No = SWR_No

not:

UPDATE SWR_Next_Available_No SET SWR_No = SWR_No + 1, @Next_No = SWR_No + 1

The second (wrong) command actually increments the number one extra time.  It has already been incremented and does not need to be incremented twice.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.