SqlServer查看死锁的存储过程
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[sp_who_lock]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[sp_who_lock]
GO
USE master
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 '现在没有阻塞和死锁信息' 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
END
DROP TABLE #tmp_lock_who
RETURN 0
END
exec master.dbo.sp_who_lock
DROP PROCEDURE [dbo].[sp_who_lock]
GO
USE master
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 '现在没有阻塞和死锁信息' 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
END
DROP TABLE #tmp_lock_who
RETURN 0
END
exec master.dbo.sp_who_lock
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【腾讯课堂】:https://abennet.ke.qq.com
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET