HOLDLOCK

HOLDLOCK is an SQL hint that instructs the database to hold a shared lock until the transaction is completed, thereby preventing other users from modifying the locked data. It ensures data consistency during the transaction process. This reduces concurrency and can slow down data operations.

Example

BEGIN TRANSACTION
SELECT * FROM Employees WITH (HOLDLOCK, ROWLOCK)
WHERE EmployeeID = 1
ROLLBACK TRANSACTION

Output

EmployeeID | FirstName | LastName
-----------|-----------|---------
1 | John | Doe

Explanation

The above SQL code starts a transaction and holds a lock on a specific row in the Employees table where the EmployeeID is 1. The lock holds until the transaction is complete. After selecting the desired data, the transaction is rolled back, releasing the lock.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.