/*--Handling deadlock
View the current process or deadlock process, and can automatically kill the dead process
Because it is for deadlock, so if there is a deadlock process, you can only view the deadlock process
Of course, you can control it through parameters. No matter whether there is a deadlock or not, you will only view the deadlock process
--Zou Jian 2004.4--*/
/*--Call example
exec p_lockinfo
--*/
create proc p_lockinfo
@kill_lock_spid bit=1, --Whether to kill the deadlocked process, 1 kill, 0 only display
@show_spid_if_nolock bit=1 --If there is no deadlock process, whether to display normal process information, 1 to display, 0 not to display
as
declare @count int,@s nvarchar(1000),@i int
select id=identity(int,1,1 ), flag,
process ID=spid, thread ID=kpid, block process ID=blocked, database ID=dbid,
database name=db_name(dbid), user ID=uid, username=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 , network card address=net_address
into #t from(
select flag='deadlock process',
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 flags='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(30),b Int,EventInfo nvarchar(255))
if @kill_lock_spid=1