SQL SERVER 死鎖查詢儲存

2021-07-11 03:08:18 字數 1903 閱讀 6154

– exec sp_who_lock 查詢哪個庫的死鎖,儲存就建立在哪個庫上

if exists (select * from sys.objects where object_id = object_id(n』[dbo].[sp_who_lock]』) and type in (n』p』, n』pc』))

drop procedure [dbo].[sp_who_lock]

gocreate procedure [dbo].[sp_who_lock]

as begin

declare @spid int,@bl int,

@inttransactioncountonentry int,

@introwcount int,

@intcountproperties int,

@intcounter int,

@hostname varchar(255)

create table #tmp_lock_who (

id int identity(1,1),

spid smallint,

bl smallint,

hostname varchar(255))

if @@error<>0 return @@error

insert into #tmp_lock_who(spid,bl,hostname)

select 0 ,blocked,hostname

from (select * from master.dbo.sysprocesses where blocked>0 ) a

where not exists(select * from (select * from master.dbo.sysprocesses where blocked>0 ) b

where a.blocked=spid)

union select spid,blocked,hostname from master.dbo.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 n』現在沒有阻塞和死鎖資訊』 as message

– 迴圈開始

while @intcounter <= @intcountproperties

begin

– 取第一條記錄

select @spid = spid,@bl = bl,@hostname = hostname

from #tmp_lock_who where id = @intcounter

begin

if @spid =0

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

else

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

dbcc inputbuffer (@bl )

end– 迴圈指標下移

set @intcounter = @intcounter + 1

enddrop table #tmp_lock_who

return 0

endgo

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 ...

SqlServer死鎖處理儲存過程

處理死鎖 檢視當前程序,或死鎖程序,並能自動殺掉死程序 因為是針對死的,所以如果有死鎖程序,只能檢視死鎖程序 當然,你可以通過引數控制,不管有沒有死鎖,都只檢視死鎖程序 鄒建 2004.4 呼叫示例 exec p lockinfo alter proc p lockinfo kill lock sp...

SqlServer儲存過程分頁查詢

alter procedure dbo p common pagination currentpage int,當前頁碼 pagesize int,每頁記錄數 querysql nvarchar max 查詢sql ordersql nvarchar max 排序sql totalsql nvarc...