Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Lock a row after SELECT and release after UPDATE

$
0
0

Hi All,

Here’s what I’m trying to achieve.

DDL -

IF OBJECT_ID(N'FirstTab', N'U') IS NOT NULL
    DROP TABLE FirstTab;
GO
CREATE TABLE FirstTab
(Id INT IDENTITY(1,1) PRIMARY KEY
, StartVal INT,EndVal INT)
INSERT INTO FirstTab (StartVal,EndVal)
VALUES (10,100)

Script -


BEGIN TRANSACTION
--Step 1 –
SELECT EndVal
FROM FirstTab
WHERE Id=1
--Step 2–Use the ‘EndValue’from Step1 in a script and this script 
generates a different 'EndVal' (say 500)
--Step 3 – 
UPDATE FirstTab
SET EndVal=500
WHERE Id=1
COMMIT TRANSACTION

Question -

How do I place a lock after Step1 and release it after Step 3 i.e. I want to prohibit other processes/scripts in Reading/Updating that ROW (Id=1)

Environment -

Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)

Developer Edition (64-bit)

ProductVersion : 10.50.4000.0

Product Level : SP2


Narsimha


Viewing all articles
Browse latest Browse all 23857

Trending Articles