檢視資料庫死鎖的儲存過程SP WHO LOCK

2021-04-20 13:30:10 字數 3505 閱讀 1964

檢視資料庫死鎖的儲存過程sp_who_lock

create  procedure sp_who_lock

asbegin

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 a.blocked=spid)

union select spid,blocked from sysprocesses where  blocked>0

if @@error<>0 return @@error 

-- 找到臨時表的記錄數

select  @intcountproperties = count(*),@intcounter = 1

from #tmp_lock_who

if @@error<>0 return @@error 

if @intcountproperties=0

select '現在沒有阻塞和死鎖資訊' as message

-- 迴圈開始

while @intcounter <= @intcountproperties

begin

-- 取第一條記錄

select  @spid = spid,@bl = bl

from #tmp_lock_who where id = @intcounter

begin

if @spid =0 

select '引起資料庫死鎖的是: '+ cast(@bl as varchar(10)) + '程序號,其執行的sql語法如下'

else

select '程序號spid:'+ cast(@spid as varchar(10))+ '被' + '程序號spid:'+ cast(@bl as varchar(10)) +'阻塞,其當前程序執行的sql語法如下'

dbcc inputbuffer (@bl )

end 

-- 迴圈指標下移

set @intcounter = @intcounter + 1

enddrop table #tmp_lock_who

return 0

end--使用於sql server 2000

create procedure sp_lock --- 1996/04/08 00:00

@spid1 int = null,  /* server process id to check for locks */

@spid2 int = null  /* other process id to check for locks */

asset nocount on

/***  show the locks for both parameters.

*/if @spid1 is not null

begin

select  convert (smallint, req_spid) as spid,

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,

master.dbo.spt_values v,

master.dbo.spt_values x,

master.dbo.spt_values u

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 req_spid in (@spid1, @spid2)

end/*

**  no parameters, so show all the locks.

*/else

begin

select  convert (smallint, req_spid) as spid,

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,

master.dbo.spt_values v,

master.dbo.spt_values x,

master.dbo.spt_values u

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'

order by spid

endreturn (0) -- sp_lock

go微軟已經寫好的sp_who,為啥不用?你能保證你寫的儲存過程的效能比微軟自己寫的效能要好?

sqlserver查詢資料庫死鎖儲存過程 sql

use xx庫 go object storedprocedure dbo sp who lock script date 01 01 2014 14 52 27 set ansi nulls on goset quoted identifier on gocreate procedure dbo ...

查詢資料庫死鎖語句儲存過程

use master go object storedprocedure dbo p lockinfo script date 2016 11 23 16 29 01 set ansi nulls on goset quoted identifier on go 處理死鎖 檢視當前程序,或死鎖程序,...

檢視死鎖儲存過程

object storedprocedure dbo sp who lock script date 08 13 2014 12 52 08 set ansi nulls on goset quoted identifier on goalter procedure dbo sp who lock ...