Function description of the "locking option" in the SELECT statement
SQL Server provides a powerful and complete locking mechanism to help achieve concurrency and high performance of the database system. Users can either use the default settings of SQL Server or use the "locking option" in the select statement to achieve the desired effect.
This article introduces the various "locking options" in the SELECT statement and the corresponding function descriptions.
Function description:
NOLOCK (no locking)
When this option is selected, SQL Server does not add any locks when reading or modifying data.
In this case, the user may read the data in the unfinished transaction (Uncommited Transaction) or rollback (Roll Back), which is the so-called "dirty data".
HOLDLOCK
When this option is selected, SQL Server will hold this shared lock until the end of the entire transaction without releasing it on the way.
UPDLOCK (modification lock)
When this option is selected, SQL Server uses a modification lock instead of a shared lock when reading data, and holds this lock until the end of the entire transaction or command. Using this option ensures that multiple processes can read data at the same time but only this process can modify the data.
TABLOCK (table lock)
When this option is selected, SQL Server will set a shared lock on the entire table until the command ends.
This option ensures that other processes can only read but not modify the data.
PAGLOCK (Page Lock)
This option is the default option. When selected, SQL Server uses shared page locks.
TABLOCKX (exclusive table lock)
When this option is selected, SQL Server will set an exclusive lock on the entire table until the command or transaction ends. This will prevent other processes from reading or modifying the data in the table.
Using these options will cause the system to ignore the transaction isolation level (Transaction Isolation Level) originally set in the SET statement.
Please consult the SQL Server online manual for more information.