[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
1SET QUOTED_IDENTIFIEROn2GO3SET ANSI_NULLSOn4GO5CREATEPROCEDURESp_who_lock6As7BEGIN8DECLARE@spidInt,9@blInt,10@intTransactionCountOnEntryInt,11@intRowcountInt,12@intCountPropertiesInt,13@intCounterInt14CREATETABLE#tmp_lock_who15(IdIntIDENTITY (1,1) ,spidSMALLINT,BlSMALLINT19)20IF@ @ERROR<>021stRETURN@ @ERROR22INSERTInto#tmp_lock_who (spid, BL)23SELECT0, blocked24From (SELECT*25FromSys.sysprocesses26WHERE blocked>027) A28WHERENotEXISTS (SELECT*29From (SELECT*30FromSys.sysprocesses31WHERE blocked>032) b33WHERE a.blocked=SPID)34UNION35SELECTSPID, blocked36FromSys.sysprocesses37WHERE blocked>038IF@ @ERROR<>039RETURN@ @ERROR40--Number of records found for temporary table41SELECT@intCountProperties=COUNT (*),@intCounter=142From#tmp_lock_who43IF@ @ERROR<>044RETURN@ @ERROR45IF@intCountProperties=046SELECT N‘No blocking and deadlock information now‘AsMessage47--Cycle start48While@intCounter<=@intCountProperties49BEGIN50--Take the first record51SELECT@spid= spid,@bl=Bl52From#tmp_lock_who53WHERE Id=@intCounter54BEGIN55IF@spid=056SELECT N‘Causing the database deadlock is:‘+CAST (@blAsVARCHAR (10))57+ N‘Process number, which executes the following SQL syntax‘58ELSE59SELECT N‘Process number spid:‘+CAST (@spidAsVARCHAR (10))60+ N‘By the process number spid:‘+CAST (@blAsVARCHAR ((ten)) N‘The SQL syntax that the current process executes is blocked as follows‘61DBCC InputBuffer (@bl)62END63--The loop pointer moves down64SET@intCounter=@intCounter+165END66DROPTABLE#tmp_lock_who67RETURN068END sp_who_lock Go, EXEC , DROP PROC sp_who_lock, go,   SET QUOTED_IDENTIFIER OFF ansi_nulls go  SET    to On  

[SQL] Deadlock processing statement

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.