SQL2005查看死锁存储过程sp_who_lock

作者:yourber 时间:2024-01-12 20:34:13 

下面是我整理的监控sql server数据库,在性能测试过程中是否出现死锁、堵塞的SQL语句,还算比较准备,留下来备用。

调用方法:选中相应的数据库,执行exec sp_who_lock


USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE procedure [dbo].[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
标签:死锁,sp,who,lock
0
投稿

猜你喜欢

  • Python文件操作函数用法实例详解

    2021-03-12 07:10:29
  • Python包,__init__.py功能与用法分析

    2021-06-17 22:19:54
  • 详解Vue.js 可拖放文本框组件的使用

    2024-04-27 15:47:22
  • python用字符组成图像代码实例

    2023-09-15 02:12:04
  • JS实现图片手风琴效果

    2023-08-23 19:28:27
  • 基于python模拟TCP3次握手连接及发送数据

    2022-04-02 02:40:33
  • yolov5返回坐标的方法实例

    2023-10-05 20:09:43
  • vue3如何实现挂载并使用axios

    2023-07-02 16:46:06
  • Python的时间模块datetime详解

    2023-10-17 01:36:48
  • SQL查询语句优化的实用方法总结

    2024-01-25 18:51:21
  • OpenCV-Python实现人脸美白算法的实例

    2023-03-27 00:33:39
  • js中判断数字\\字母\\中文的正则表达式 (实例)

    2024-04-10 10:56:07
  • Python fire模块(最简化命令行生成工具)的使用教程详解

    2022-06-10 15:25:00
  • python图片合成的示例

    2023-11-17 17:09:36
  • 《用户体验的要素》摘记

    2008-08-04 17:59:00
  • Mysql 增加主键或者修改主键的sql语句操作

    2024-01-20 00:22:02
  • 友情连接地址代码-线线表格

    2010-07-01 16:26:00
  • PID原理与python的简单实现和调参

    2021-08-13 13:27:36
  • python3.4下django集成使用xadmin后台的方法

    2022-05-09 19:36:42
  • python抓取网页内容并进行语音播报的方法

    2021-03-08 14:02:53
  • asp之家 网络编程 m.aspxhome.com