Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOLDLOCK OR SET CONCURRENCY 1

Status
Not open for further replies.

TitleistDBA

IS-IT--Management
Apr 22, 2002
162
US
I am having a problem with the following proc. Two processes can call the proc and they both get the same next id. I need to lock the record when I select it. Does anybody know how to use HOLDLOCK or SET CONCURRENCY LOCKCC.


CREATE PROCEDURE Webm_GetNextTransactionID
@Transaction_Type varchar(10),
@Next_Avail_Transaction_ID int OUTPUT,
@RtnMessage varchar(250) OUTPUT,
@Successind int OUTPUT
AS

DECLARE @err int
DECLARE @rowcnt int

SELECT @Successind = 1
SELECT @RtnMessage = ' '

/* This procedure retrieves the last transaction ID, increments and returns the transaction ID to the caller */

BEGIN TRANSACTION

/* Get Next Up Number from table */
SELECT @Next_Avail_Transaction_ID = Next_Avail_Transaction_ID from Transaction_ID_Next_Up where Transaction_Type = @Transaction_Type

SELECT @err = @@ERROR, @rowcnt = @@ROWCOUNT
IF( @rowcnt = 0 )
BEGIN
SELECT @RtnMessage = 'Transaction_ID_Next_Up row not found to increment'
SELECT @Successind = 0
END
IF( @err <> 0 )
BEGIN
SELECT @RtnMessage = description FROM master.dbo.sysmessages WHERE error = @err
SELECT @Successind = 0
END

/* If you got a record, increment and update */
IF (@rowcnt = 1)
BEGIN

SELECT @Next_Avail_Transaction_ID = @Next_Avail_Transaction_ID + 1
UPDATE Transaction_ID_Next_Up SET Next_Avail_Transaction_ID = @Next_Avail_Transaction_ID where Transaction_Type = @Transaction_Type

SELECT @err = @@ERROR, @rowcnt = @@ROWCOUNT
IF( @rowcnt = 0 )
BEGIN
SELECT @RtnMessage = 'Transaction_ID_Next_Up row not updated'
SELECT @Successind = 0
END
IF( @err <> 0 )
BEGIN
SELECT @RtnMessage = description FROM master.dbo.sysmessages WHERE error = @err
SELECT @Successind = 0
END

END


COMMIT

GO


 
If you don't have INSERTs, HOLDLOCK is equivalent to isolation level 2. Therefore try:
Code:
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRANSACTION
...

------
Math problems? Call 0800-[(10x)(13i)^2]-[sin(xy)/2.362x]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top