SQL檢查死鎖情況

2022-07-04 17:51:08 字數 2478 閱讀 8258

create

procedure

sp_who_lock

asbegin

declare

@spid

intdeclare

@blk

intdeclare

@count

intdeclare

@index

intdeclare

@lock

tinyint

set@lock=0

create

table

#temp_who_lock

( id

intidentity(1,1

), spid

int,

blk

int)

if@@error

<>

0return

@@error

insert

into

#temp_who_lock(spid,blk)

select

0,blocked

from (select

*from master..sysprocesses where blocked>

0)a

where

notexists(select

*from master..sysprocesses where a.blocked =spid and blocked>0)

union

select spid,blocked from master..sysprocesses where blocked>0if

@@error

<>

0return

@@error

select

@count

=count(*),@index=1

from

#temp_who_lock

if@@error

<>

0return

@@error

if@count=0

begin

select

'沒有阻塞和死鎖資訊

'return

0end

while

@index

<=

@count

begin

ifexists(select

1from #temp_who_lock a where id>

@index

andexists(select

1from #temp_who_lock where id<=

@index

and a.blk=

spid))

begin

set@lock=1

select

@spid

=spid,@blk

=blk from #temp_who_lock where id=

@index

select

'引起資料庫死鎖的是: '+

cast(@spid

asvarchar(10)) +

'程序號,其執行的sql語法如下

'select

@spid, @blk

dbcc inputbuffer(@spid

) dbcc inputbuffer(@blk

) end

set@index

=@index+1

endif

@lock=0

begin

set@index=1

while

@index

<=

@count

begin

select

@spid

=spid,@blk

=blk from #temp_who_lock where id=

@index

if@spid=0

select

'引起阻塞的是:'+

cast(@blk

asvarchar(10))+

'程序號,其執行的sql語法如下

'else

select

'程序號spid:'+

cast(@spid

asvarchar(10))+'被

'+'程序號spid:'+

cast(@blk

asvarchar(10)) +

'阻塞,其當前程序執行的sql語法如下

'dbcc inputbuffer(@spid

) dbcc inputbuffer(@blk

) set

@index

=@index+1

endend

drop

table

#temp_who_lock

return

0end

goexec sp_who_lock

ORACLE檢查死鎖

select sql hash value from v session where sid in select session id from v locked object 四 死鎖的解決方法 一般情況下,只要將產生死鎖的語句提交就可以了,但是在實際的執行過程中。使用者可 能不知道產生死鎖的語句...

Oracle中檢查死鎖

select v session.sid,v session.serial v process.spid,rtrim object type object type,rtrim owner object name object name,decode lmode,0,none 1,null 2,ro...

oracle死鎖檢查方法

用dba登陸 sqlplus nolog conn as sysdba select username,lockwait,status,machine,program from v session where sid in select session id from v locked object...