This article is a mirror article of machine translation, please click here to jump to the original article.

View: 8936|Reply: 0

SQL locks NOLOCK, HOLDLOCK, UPDLOCK, TABLOCK, TABLOCKX

[Copy link]
Posted on 7/20/2016 2:02:51 PM | | |

NOLOCK
When this option is selected, SQL Server reads or modifies data without any locks. In this case, the user may read data from an Uncommitted Transaction or Roll Back, known as "dirty data".

HOLDLOCK
When this option is selected, SQL Server maintains this shared lock until the end of the entire transaction without releasing it on the way.

UPDLOCK
When this option is selected, SQL Server uses a modify lock instead of a shared lock when reading data and maintains this lock until the end of the entire transaction or command. This option ensures that multiple processes can read data at the same time, but only that process can modify the data.

TABLOCK
When this option is selected, SQL Server will place a shared lock on the entire table until the command ends. This option ensures that other processes can only read and not modify the data.

PAGLOCK
This option is the default option, and when selected, SQL Server uses a shared page lock.

TABLOCKX (Exclusive Table Lock)
When this option is selected, SQL Server will place a 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.

HOLDLOCK holds the shared lock until the entire transaction is complete and should be released as soon as the locked object is not needed, equal to the SERIALIZABLE transaction isolation level

The NOLOCK statement is executed without issuing a shared lock, allowing dirty reads, which is equal to the READ UNCOMMITTED transaction isolation level

PAGLOCK uses multiple page locks where one table lock is used

READPAST lets the sql server skip any locked lines and execute transactions, and for READ UNCOMMITTED transaction isolation levels, only skip RID locks, not page, zone, and table locks

ROWLOCK enforces the use of rowlocks

TABLOCKX enforces the use of an exclusive table-level lock, which prevents any other transaction from using the table during the transaction

UPLOCK forces the use of updates when reading a table without a shared lock

Note: The difference between locking a table in a database
SELECT * FROM table WITH (HOLDLOCK) Other transactions can read the table, but cannot update or delete
SELECT * FROM table WITH (TABLOCKX) Other transactions cannot read, update, and delete tables




Previous:Locks, dirty reads, unrepeatable reads, and false reads in SQL
Next:SQL Server looks at the deadlock and releases the deadlock
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com