alter procedure [dbo].[sp_lock_check]
@spid1 int = null,
@spid2 int = null
asset nocount on
if @spid1 is not null
begin
select convert (smallint, req_spid) as spid,
loginame=rtrim(loginame),hostname ,t2.name,
rsc_dbid as dbid,
rsc_objid as objid,
rsc_indid as indid,
substring (v.name, 1, 4) as type,
substring (rsc_text, 1, 16) as resource,
substring (u.name, 1, 8) as mode,
substring (x.name, 1, 5) as status
from master.dbo.syslockinfo(nolock),
master.dbo.spt_values v(nolock),
master.dbo.spt_values x(nolock),
master.dbo.spt_values u(nolock),
master.dbo.sysprocesses t1(nolock),
sysobjects t2(nolock)
where master.dbo.syslockinfo.rsc_type = v.number
and v.type = 'lr'
and master.dbo.syslockinfo.req_status = x.number
and x.type = 'ls'
and master.dbo.syslockinfo.req_mode + 1 = u.number
and u.type = 'l'
and t1.spid=convert (smallint, req_spid)
and req_spid in (@spid1, @spid2)
and rsc_objid =t2.id
endelse
begin
select convert (smallint, req_spid) as spid,
loginame=rtrim(loginame),hostname ,t2.name,
rsc_dbid as dbid,
rsc_objid as objid,
rsc_indid as indid,
substring (v.name, 1, 4) as type,
substring (rsc_text, 1, 16) as resource,
substring (u.name, 1, 8) as mode,
substring (x.name, 1, 5) as status
from master.dbo.syslockinfo(nolock),
master.dbo.spt_values v(nolock),
master.dbo.spt_values x(nolock),
master.dbo.spt_values u(nolock),
master.dbo.sysprocesses t1(nolock),
sysobjects t2(nolock)
where master.dbo.syslockinfo.rsc_type = v.number
and v.type = 'lr'
and master.dbo.syslockinfo.req_status = x.number
and x.type = 'ls'
and master.dbo.syslockinfo.req_mode + 1 = u.number
and u.type = 'l'
and t1.spid=convert (smallint, req_spid)
and rsc_objid =t2.id
order by spid
select convert (smallint, req_spid) as spid,
loginame=rtrim(loginame),hostname ,t2.name,
rsc_dbid as dbid,
rsc_objid as objid,
rsc_indid as indid,
substring (v.name, 1, 4) as type,
substring (rsc_text, 1, 16) as resource,
substring (u.name, 1, 8) as mode,
substring (x.name, 1, 5) as status
into #temp
from master.dbo.syslockinfo(nolock),
master.dbo.spt_values v(nolock),
master.dbo.spt_values x(nolock),
master.dbo.spt_values u(nolock),
master.dbo.sysprocesses t1(nolock),
sysobjects t2(nolock)
where master.dbo.syslockinfo.rsc_type = v.number
and v.type = 'lr'
and master.dbo.syslockinfo.req_status = x.number
and x.type = 'ls'
and master.dbo.syslockinfo.req_mode + 1 = u.number
and u.type = 'l'
and t1.spid=convert (smallint, req_spid)
and rsc_objid =t2.id
order by spid
declare dbcc_inputbuffer cursor
read_only
for select distinct spid from #temp
declare @spid int
open dbcc_inputbuffer
fetch next from dbcc_inputbuffer into @spid
while (@@fetch_status <> -1)
begin
if (@@fetch_status <> -2)
begin
-- print 'add user defined code here'
-- eg.
select @spid
dbcc inputbuffer(@spid)
endfetch next from dbcc_inputbuffer into @spid
endclose dbcc_inputbuffer
deallocate dbcc_inputbuffer
endreturn (0) -- sp_lock
SQL 死鎖處理語句
1 set quoted identifier on2go 3set ansi nulls on4go 5create procedure sp who lock6as 7begin 8declare spid int,9 bl int,10 inttransactioncountonentry i...
檢視死鎖的SQL語句
use master go declare spid int,bl int declare s cur cursor for select 0 blocked from select from sysprocesses where blocked 0 a where not exists selec...
Sql Server 檢測死鎖的SQL語句
首先建立乙個標量值函式diglock,用來遞迴檢測sqlserver中的每乙個會話是否存在加鎖迴圈,如果該函式最終返回1則表示檢測到了加鎖迴圈 也就是說檢測到了死鎖 如果最終返回0則表示沒有檢測到加鎖迴圈。1 create function dbo diglock 2 3 spid int,4 or...