I wrote a stored procedure the other day and used transactions in the stored procedure.CodeComment out to debug and find the error. Suddenly, a table is locked. It turns out that the code for creating the transaction has forgotten to comment out. Solution to table lock in this article.
There are many other scenarios that cause a deadlock in the Table. Unlocking is actually very simple. Here is an example:
1. First create a test table: Create Table Test
(
TID int
Identity (1, 1)
)
2. Execute the following SQL statement to lock the table:
Select * from test with (tablockx)
3. Use the following statement to check which tables in the current database have deadlocks:
Select request_session_id
Spid, object_name (resource_associated_entity_id) tablename
From
SYS. dm_tran_locks
Where resource_type = 'object'
4. The execution result of the preceding statement is as follows:
Spid: the ID of the locked process.
Tablename: name of the table in which a deadlock occurs.
5. You only need to use the kill keyword to kill the locked process ID to unlock the table: