SQL Server, kill the deadlock process

Source: Internet
Author: User
Tags rowcount

Use [ERPDB1]
GO
/****** object:storedprocedure [dbo].    [P_lockinfo_mykill] Script date:12/26/2014 14:59:58 ******/
SET ANSI_NULLS on
GO
SET QUOTED_IDENTIFIER OFF
GO


ALTER proc [dbo]. [P_lockinfo_mykill]
@kill_lock_spid Bit=1,--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 (+), @i int
Select Id=identity (int,1,1), logo,
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, open transaction number =open_tran, process status =status,
Workstation name =hostname, application name =program_name, workstation process id=hostprocess,
Domain =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 @[email protected] @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 @[email protected] @rowcount
End

If @count >0
Begin
CREATE table #t1 (ID int identity (int,eventinfo), a nvarchar (255), b nvarchar
If @kill_lock_spid =1
Begin
DECLARE @spid varchar (10), @ Flag varchar (10)
While @i<[email protected]
Begin
Select @spid = Process id,@ flag = flag from #t where[email protected]
Insert #t1 EXEC (' DBCC INPUTBUFFER ('[email protected]+ ') ')
If @ sign = ' deadlock process ' exec (' kill '[email protected])
Set @[email protected]+1
End
End
Else
While @i<[email protected]
Begin
Select @s= ' DBCC INPUTBUFFER (' +cast (process ID as varchar) + ') ' from #t where[email protected]
Insert #t1 EXEC (@s)
Set @[email protected]+1
End
Select a.*, the SQL statement for the process =b.eventinfo
From #t a join #t1 B on a.id=b.id
End

SQL Server, kill the deadlock process

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.