Workaround for SQL Server table deadlock

Source: Internet
Author: User

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

Related Article

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.