Solved

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

Posted on 2006-06-17
7
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Expert Comment

by:TRACEYMARY
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
Thanks for helping me think this out.  Whew, glad that is over...
0
 
LVL 7

Author Comment

by:EYoung
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now