查詢資料庫中程序死鎖情況指令碼。

2021-06-06 02:16:50 字數 2554 閱讀 9402

查詢資料庫中程序死鎖情況,指令碼。

use [master]

go/****** object: storedprocedure [dbo].[sp_who_lock] script date: 04/26/2012 11:13:51 ******/

set ansi_nulls on

goset quoted_identifier on

gocreate procedure [dbo].[sp_who_lock]

asbegin

declare @spid int,

@bl int,

@inttransactioncountonentry int,

@introwcount int,

@intcountproperties int,

@intcounter int

declare @tmp_lock_who table

( id int identity(1,1),

spid smallint,

bl smallint )

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

-- 找到臨時表的記錄數

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

from @tmp_lock_who

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

endend

其它查詢正在執行程序資訊

with tbas(

select blocking_session_id,session_id,db_name(database_id) as dbname, [text]

from master.sys.dm_exec_requests a

),tb1 as

( select a.*,login_time,program_name,client_inte***ce_name,login_name,cpu_time,memory_usage*8 as 'memory_usage(kb)',

total_scheduled_time,reads,writes,logical_reads

from tb a

inner join master.sys.dm_exec_sessions b on a.session_id=b.session_id

)select a.*,connect_time,client_tcp_port,client_net_address

from tb1 a inner join master.sys.dm_exec_connections b on a.session_id=b.session_id

go-- 查出互相死鎖的程序資訊

use master

goselect a.*

from sysprocesses a, sysprocesses b

where a.blocked > 0 and

b.blocked > 0 and

a.spid != b.spid and

a.blocked = b.spid and

b.blocked = a.spid

go

資料庫死鎖查詢

檢視資料庫鎖 use master go object storedprocedure dbo sp who lock script date 2019 2 18 11 17 50 set ansi nulls on goset quoted identifier on goalter proced...

檢視資料庫死鎖程序

回答 use master 必須在master資料庫中建立 goif exists select from dbo.sysobjects where id object id n dbo p lockinfo and objectproperty id,n isprocedure 1 drop pr...

Kill 資料庫死鎖程序

exec a lockinfo kill lock spid 0,show spid if nolock 0 exec a lockinfo kill lock spid 1,show spid if nolock 0 alter proc dbo a lockinfo kill lock spid...