[SQL] Deadlock processing statement

Source: Internet
Author: User

Introduction

Today in the group to see the resolution of the deadlock of the SQL statement, just think of this thing will definitely use, download down, here to record, later find also convenient.

Sql
1 SETQuoted_identifier on2 GO3 SETAnsi_nulls on4 GO5 CREATE PROCEDURESp_who_lock6  as7     BEGIN8         DECLARE @spid INT ,9             @bl INT ,Ten             @intTransactionCountOnEntry INT , One             @intRowcount INT , A             @intCountProperties INT , -             @intCounter INT -         CREATE TABLE#tmp_lock_who the             ( -IdINT IDENTITY(1,1) , -spidSMALLINT , -BlSMALLINT +             ) -          IF @ @ERROR <> 0 +             RETURN @ @ERROR A         INSERT  into#tmp_lock_who (spid, BL) at                 SELECT 0, blocked -                      from(SELECT * -                              fromsys.sysprocesses -                             WHEREBlocked> 0 - ) A -                     WHERE  not EXISTS(SELECT * in                                          from(SELECT * -                                                  fromsys.sysprocesses to                                                 WHEREBlocked> 0 + ) b -                                         WHEREA.blocked=spid) the                 UNION *                 SELECTspid, blocked $                      fromsys.sysprocessesPanax Notoginseng                     WHEREBlocked> 0 -         IF @ @ERROR <> 0 the             RETURN @ @ERROR +        --number of records found for temporary table A         SELECT @intCountProperties = COUNT(*),@intCounter = 1 the              from#tmp_lock_who +          IF @ @ERROR <> 0 -             RETURN @ @ERROR $         IF @intCountProperties = 0 $             SELECTN'No blocking and deadlock information now'  asmessage -        --Cycle Start -          while @intCounter <= @intCountProperties the             BEGIN -               --take the first recordWuyi                 SELECT @spid =spid@bl =BL the                      from#tmp_lock_who -                     WHEREId= @intCounter Wu                 BEGIN -                     IF @spid = 0 About                         SELECTN'causing the database deadlock is:' + CAST(@bl  as VARCHAR(Ten)) $                                 +N'process number, which executes the following SQL syntax' -                     ELSE -                         SELECTN'process number spid:' + CAST(@spid  as VARCHAR(Ten)) -                                 +N'by the process number spid:' + CAST(@bl  as VARCHAR(Ten)) N'The SQL syntax that the current process executes is blocked as follows' A                     DBCCInputBuffer (@bl ) +                 END the               --The loop pointer moves down -                 SET @intCounter = @intCounter + 1 $             END the         DROP TABLE#tmp_lock_who the         RETURN 0 the     END the Go - EXECSp_who_lock in DROP PROCSp_who_lock the GO the SETQuoted_identifierOFF About GO the  the SETAnsi_nulls on the  + GO

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.