Solved

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

Posted on 2006-06-17
7
236 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL eating up memory? 16 42
How come this XML node is not read? 3 31
Searching a barcode number within a string. 7 27
SQL Syntax 6 32
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

726 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