?
Solved

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

Posted on 2006-06-17
7
Medium Priority
?
256 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:EYoung
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:TRACEYMARY
ID: 16928083
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
 
LVL 7

Author Comment

by:EYoung
ID: 16928097
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
 
LVL 7

Author Comment

by:EYoung
ID: 16928113
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 7

Expert Comment

by:TRACEYMARY
ID: 16928121
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
 
LVL 7

Accepted Solution

by:
TRACEYMARY earned 200 total points
ID: 16928125
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
 
LVL 7

Author Comment

by:EYoung
ID: 16928156
Thanks for helping me think this out.  Whew, glad that is over...
0
 
LVL 7

Author Comment

by:EYoung
ID: 16928253
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

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question