SQL 死鎖處理語句

2021-09-06 17:17:51 字數 2533 閱讀 2483

1

set quoted_identifier on2go

3set ansi_nulls on4go

5create

procedure

sp_who_lock6as

7begin

8declare

@spid

int,

9@bl

int,

10@inttransactioncountonentry

int,

11@introwcount

int,

12@intcountproperties

int,

13@intcounter

int14

create

table

#tmp_lock_who15(

16 id int

identity(1, 1

) ,17 spid smallint

,18 bl smallint19)

20if

@@error

<>021

return

@@error

22insert

into

#tmp_lock_who ( spid, bl )

23select

0, blocked

24from ( select*25

from

sys.sysprocesses

26where blocked >027

) a28

where

notexists ( select*29

from ( select*30

from

sys.sysprocesses

31where blocked >032

) b33

where a.blocked =

spid )

34union

35select

spid, blocked

36from

sys.sysprocesses

37where blocked >038

if@@error

<>039

return

@@error

40--

找到臨時表的記錄數

41select

@intcountproperties

=count(*), @intcounter=1

42from

#tmp_lock_who

43if

@@error

<>044

return

@@error

45if

@intcountproperties=0

46select n'

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

message

47--

迴圈開始

48while

@intcounter

<=

@intcountproperties

49begin

50--

取第一條記錄

51select

@spid

= spid, @bl=bl

52from

#tmp_lock_who

53where id =

@intcounter

54begin

55if

@spid=0

56select n'

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

cast(@bl

asvarchar(10

))57

+ n'

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

else

59select n'

程序號spid:'+

cast(@spid

asvarchar(10

))60

+ n'

被程序號spid:'+

cast(@bl

asvarchar(10)) n'

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

dbcc inputbuffer (@bl)62

end63

--迴圈指標下移

64set

@intcounter

=@intcounter+1

65end

66drop

table

#tmp_lock_who

67return068

end69

go70

exec

sp_who_lock

71drop

proc

sp_who_lock

72go

73set quoted_identifier off

74go

7576

set ansi_nulls on

7778

go

sql處理死鎖

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

解決SQL 死鎖的語句

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

檢視死鎖的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...