Solved

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

Posted on 2006-06-17
7
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 50 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

707 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