Write a stored procedure a few days ago, the stored procedure used a transaction, and then I put some code comments out to debug the wrong, suddenly found a table is locked, the original is the code to create a transaction forget to comment out. This table locks the workaround. In fact, not only the situation described above will lock the table, there are many kinds of scenes will make the table life and Death lock, unlocking is actually very simple, the following with an example to explain: 1 first create a test table:
CREATE TABLE Test ( TID INT IDENTITY)
2 Execute the following SQL statement to lock the table:
SELECT * from Test with (Tablockx)
3 You can see which tables in the current library are deadlocked by using the following statement:
SELECT request_session_id spid,object_name (resource_associated_entity_id) tablenamefrom
4 The result of the above statement execution is as follows:
- SPID: The process ID being locked.
- TableName: The name of the table where the deadlock occurred.
5 only use the KILL keyword to kill the locked process ID to unlock the table:
KILL 52
Workaround for SQL Server table deadlock