Database Deadlock Query Processing

Source: Internet
Author: User


CREATE PROC [dbo]. [P_lockinfo]
@kill_lock_spid BIT = 0,--whether to kill the deadlock process, 1 to kill, 0 to show only
@show_spid_if_nolock BIT = 1--If there is no deadlock process, the normal process information is displayed, 1 is displayed, 0 is not displayed
As
DECLARE @count INT,
@s NVARCHAR (MAX),
@i INT
SELECT id = IDENTITY (int,1,1),
Sign
Process id = spid,
Thread id = kpid,
Block Process id = blocked,
Database id = dbid,
Database name = db_name (dbid),
User ID = uid,
User name = Loginame,
Cumulative CPU time = CPU,
Login time = Login_time,
Number of open transactions = Open_tran,
Process status = Status,
Workstation NAME = hostname,
Application name = Program_name,
Workstation Process id = hostprocess,
Domain name = nt_domain,
Nic address = Net_address
Into #t
From (SELECT flag = ' process of deadlock ',
SPID,
Kpid,
A.blocked,
dbid,
Uid
Loginame,
Cpu
Login_time,
Open_tran,
Status,
Hostname,
Program_name,
Hostprocess,
Nt_domain,
Net_address,
S1 = A.spid,
S2 = 0
From Master. sysprocesses A
JOIN (SELECT blocked
From Master. sysprocesses
GROUP by blocked
) b on a.spid = b.blocked
WHERE a.blocked = 0
UNION All
SELECT ' |_ victim _> ',
SPID,
Kpid,
Blocked,
dbid,
Uid
Loginame,
Cpu
Login_time,
Open_tran,
Status,
Hostname,
Program_name,
Hostprocess,
Nt_domain,
Net_address,
S1 = blocked,
S2 = 1
From Master. sysprocesses A
WHERE blocked <> 0
) A
ORDER by S1,
S2
SELECT @count = @ @rowcount,
@i = 1
IF @count = 0
and @show_spid_if_nolock = 1
BEGIN
INSERT #t
SELECT flag = ' normal process ',
SPID,
Kpid,
Blocked,
dbid,
Db_name (dbid),
Uid
Loginame,
Cpu
Login_time,
Open_tran,
Status,
Hostname,
Program_name,
Hostprocess,
Nt_domain,
Net_address
From Master. sysprocesses
SET @count = @ @rowcount
END
IF @count > 0
BEGIN
CREATE TABLE #t1
(
ID INT IDENTITY (1, 1),
A NVARCHAR (MAX),
b INT,
EventInfo NVARCHAR (MAX)
)
IF @kill_lock_spid = 1
BEGIN
DECLARE @spid VARCHAR (MAX),
@ Flag VARCHAR (MAX)
While @i <= @count
BEGIN
SELECT @spid = Process ID,
@ flag = Flag
From #t
WHERE id = @i
INSERT #t1
EXEC (' DBCC INPUTBUFFER (' + @spid + ') '
)
IF @ flag = ' Deadlock process '
EXEC (' kill ' [email protected])
SET @i = @i + 1
END
END
ELSE
While @i <= @count
BEGIN
SELECT @s = ' DBCC INPUTBUFFER ('
+ CAST (Process ID as VARCHAR) + ') '
From #t
WHERE id = @i
INSERT #t1
EXEC (@s
)
SET @i = @i + 1
END
SELECT a.*,
SQL statement for process = B.eventinfo
From #t a
JOIN #t1 b on a.id = b.ID
END

Database Deadlock Query Processing

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.