Theme NexT works best with JavaScript enabled

ShunNien's Blog

不積跬步,無以致千里;不積小流,無以成江海。

0%

建立死結查詢

Introduction

建立一個預存程序,用來查詢死結(deadlock)

Conetent

建立死結查詢的預存程序,該程序名稱請自由調整,這邊是命名為 sp_who_lock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
----- 建立死結查詢
USE master;
GO
DROP PROCEDURE sp_who_lock
GO
create procedure sp_who_lock
as
begin
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
'現在沒有阻塞和Dead Lock訊息' AS message
-- 循環開始
WHILE @intCounter <= @intCountProperties
BEGIN
-- 取第一條紀錄
SELECT
@spid = spid
,@bl = bl
FROM #tmp_lock_who
WHERE id = @intCounter
BEGIN
IF @spid = 0
SELECT
'引起死結的SPID是:' + 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
end
DROP TABLE #tmp_lock_who
RETURN 0
END

建立完成後,就可以使用以下方式查詢了

1
2
---- 查詢建立的死結查詢預存
EXECUTE [master].[dbo].[sp_who_lock]

歡迎關注我的其它發布渠道