SQL Server deadlock Check Tool

Source: Internet
Author: User

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [sp_who_lock] ') and objectproperty (ID, n' isprocedure') = 1)
Drop procedure [DBO]. [sp_who_lock]
Go
/*************************************** ************************************
// Create: Fengyu mail: maggiefengyu@tom.com Date:
// Modify: From http://www.csdn.net/develop/Read_Article.asp? Id = 26566 learned and rewritten
// Description: view the blocking and deadlock conditions in the database
**************************************** ***********************************/
Use master
Go
Create procedure sp_who_lock
As
Begin
Declare @ spid int, @ BL int,
@ Inttransactioncountonentry int,
@ Introwcount int,
@ Intcountproperties int,
@ Intcounter int

Create Table # tmp_lock_who (
ID int identity (1, 1),
spid smallint,
BL smallint)
If @ error <> 0 return @ error
insert into # tmp_lock_who (spid, BL) Select 0, blocked
from (select * From sysprocesses where blocked> 0) A
where not exists (select * from (select * From sysprocesses where blocked> 0) B
where. blocked = spid)
Union select spid, blocked from sysprocesses where blocked> 0

If @ error <> 0 return @ error
-- find the number of records in the temporary table
select @ intcountproperties = count (*), @ intcounter = 1
from # tmp_lock_who
If @ error <> 0 return @ error
If @ intcountproperties = 0
select 'no blocking and deadlock information' as message

-- Start of Loop
While @ intcounter <= @ intcountproperties
Begin
-- Retrieve the first record
Select @ spid = spid, @ BL = bl
From # tmp_lock_who where id = @ intcounter
Begin
If @ spid = 0
Select '+ Cast (@ BL as varchar (10) +' causes database deadlock. The SQL statement executed by 'process No. 'is as follows'
Else
Select 'process No. spid: '+ Cast (@ spid as varchar (10) +' quilt '+' process no. spid: '+ Cast (@ BL as varchar (10 )) + 'blocking, the SQL syntax executed by the current process is as follows'
DBCC inputbuffer (@ BL)
End

-- Move the loop pointer down
Set @ intcounter = @ intcounter + 1
End

Drop table # tmp_lock_who

Return 0
End

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.