4

I need to lock a row in a table so no one can read this line while I'm running a procedure. I am using BEGIN TRAN in this procedure. So, this record I'm trying to block is uncommitted during the process. Is it possible?

Szymon
  • 42,577
  • 16
  • 96
  • 114
Felipe Pilon
  • 63
  • 1
  • 5

1 Answers1

0

Depending on what is the purpose of your stored procedure:

- In case it modifies the mentioned row, you can base on transaction levels

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

BEGIN TRANSACTION

--UPDATE/INSERT/DELETE your row here

...

COMMIT TRANSACTION SET TRANSACTION ISOLATION LEVEL REPEATABLE READ

- Use lock hints

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

BEGIN TRANSACTION

SELECT column1, column2

FROM yourTable WITH (ROWLOCK)

WHERE ID = YourRecordId

...

COMMIT TRANSACTION

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ

Community
  • 1
  • 1
bjnr
  • 3,353
  • 1
  • 18
  • 32