A simple understanding of the lock table statements in SQL (for a table)

Source: Internet
Author: User

Lock a table of a database

Copy CodeThe code is as follows:
SELECT * from table with (HOLDLOCK)


Note: The difference between a table that locks a database

Copy CodeThe code is as follows:
SELECT * from table with (HOLDLOCK)



Other transactions can read the table, but cannot update the delete

Copy CodeThe code is as follows:
SELECT * from table with (TABLOCKX)



Other transactions cannot read tables, updates, and deletes

Feature description for "Lock options" in SELECT statements

SQL Server provides a powerful and complete locking mechanism to help achieve the concurrency and high performance of the database system. Users can use the default settings of SQL Server or use the lock options in the SELECT statement to achieve the desired effect. This article describes each of the "lock options" in the SELECT statement and the corresponding feature descriptions.

Function Description:

NOLOCK (without lock)

When this option is selected, SQL Server does not add any locks when reading or modifying data. In this case, it is possible for the user to read the data in the incomplete transaction (uncommited Transaction) or rollback (roll back), known as "dirty data."

HOLDLOCK (Hold Lock)

When this option is selected, SQL Server will persist this shared lock to the end of the entire transaction and will not be released on the way.

UPDLOCK (Modify Lock)

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

TABLOCK (table Lock)

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 read only and cannot modify the data.

Paglock (page lock)

This option is the default option when SQL Server uses a shared page lock when it is selected.

Tablockx (Row it table lock)

When this option is selected, SQL Server will place an exclusive lock on the entire table until the command or transaction ends. This prevents other processes from reading or modifying the data in the table.

HOLDLOCK holds a shared lock until the entire transaction is complete and should be released immediately when the locked object is not needed, equal to the serializable transaction isolation level

NOLOCK statement execution does not emit a shared lock, allowing dirty reads, equal to the READ UNCOMMITTED transaction isolation LEVEL

Paglock with multiple page locks where a table lock is used

READPAST let SQL Server skip any locking lines, perform transactions, apply to READ UNCOMMITTED transaction isolation LEVEL only skip RID lock, skip page, zone and table lock

Rowlock forcing the use of row locks

Tablockx enforces exclusive table-level locks, which prevent any other transactions from using this table during a transaction

Uplock forcing updates to be used when reading tables without sharing locks

Note: The difference between a table that locks a database

Copy CodeThe code is as follows:
SELECT * from table with (HOLDLOCK) Other transactions can read the table but cannot update the delete

SELECT * from table with (Tablockx) Other transactions cannot read table, update and delete

The code is as follows:
SELECT * from table with (HOLDLOCK)

Note: The difference between a table that locks a database

Copy CodeThe code is as follows:
SELECT * from table with (HOLDLOCK)

Other transactions can read the table, but cannot update the delete

Copy CodeThe code is as follows:
SELECT * from table with (TABLOCKX)

Other transactions cannot read tables, updates, and deletes
Feature description for "Lock options" in SELECT statements
SQL Server provides a powerful and complete locking mechanism to help achieve the concurrency and high performance of the database system. Users can use the default settings of SQL Server or use the lock options in the SELECT statement to achieve the desired effect. This article describes each of the "lock options" in the SELECT statement and the corresponding function descriptions

To view the locked table:

Copy CodeThe code is as follows:
Select request_session_id spid,object_name (resource_associated_entity_id) tableName
From sys.dm_tran_locks where resource_type= ' OBJECT '

SPID Lock Table Process
TableName Locked Table name
[@[email protected]]

Unlock:

Create a temporary table

?
1234567891011 CREATE TABLE #HarveyLock(SPID INT,DBID INT,OBJID INT,INDID INT,TYPE VARCHAR(100),RESOURCE VARCHAR(100),MODE VARCHAR(100),STATUS VARCHAR(100))

Save the lock information to the table
INSERT into #HarveyLock EXEC sp_lock

3. The lock that is obtained in the condition query in table
SELECT * from #HarveyLock

4.KILL related lock
KILL @SPID
Cases

?
12345 declare @spid int set @spid = Page --lock table process declare @sql varchar set @sql = + cast (@spid as varchar ) exec (@sql)

A simple understanding of the lock table statements in SQL (for a table)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.