博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql 查找死锁对象的存储过程
阅读量:7226 次
发布时间:2019-06-29

本文共 1599 字,大约阅读时间需要 5 分钟。

1 USE [master] 2 GO 3 /****** Object:  StoredProcedure [dbo].[sp_who_lock]    Script Date: 05/12/2016 14:13:46 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8  9 10 ALTER procedure [dbo].[sp_who_lock] 11 as 12 begin 13     --exec sp_who_lock14     15 declare @spid int, @bl int, @intTransactionCountOnEntry int, @intRowcount int, @intCountProperties int, @intCounter int 16 create table #tmp_lock_who ( 17  id int identity(1,1), 18  spid smallint, 19  bl smallint20 ) 21 IF @@ERROR<>0 RETURN @@ERROR 22 insert into #tmp_lock_who(spid,bl) select 0 ,blocked 23  from (select * from sysprocesses where blocked>0 ) a 24  where not exists(select * from (select * from sysprocesses where blocked>0 ) b 25  where a.blocked=spid) 26  union select spid,blocked from sysprocesses where blocked>0 27 IF @@ERROR<>0 RETURN @@ERROR 28 29 -- 找到临时表的记录数 30 select @intCountProperties = Count(*),@intCounter = 1 31 from #tmp_lock_who 32 IF @@ERROR<>0 RETURN @@ERROR 33 if @intCountProperties=0 34  select '现在没有阻塞和死锁信息' as message 35 -- 循环开始 36 while @intCounter <= @intCountProperties 37 begin 38 -- 取第一条记录 39 select @spid = spid,@bl = bl 40  from #tmp_lock_who where Id = @intCounter 41  begin 42  if @spid =0 43  select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下' 44 else 45  select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下' 46 DBCC INPUTBUFFER (@bl ) 47  end 48 -- 循环指针下移 49 set @intCounter = @intCounter + 1 50 end 51 drop table #tmp_lock_who 52 return 0 53 end

 

转载于:https://www.cnblogs.com/ziranquliu/p/5485503.html

你可能感兴趣的文章
简易sqlhelper-java
查看>>
通过案例对SparkStreaming 透彻理解三板斧之一:解密SparkStreaming运行机制
查看>>
HBuilder 学习笔记
查看>>
利用OpenStreetMap(OSM)数据搭建一个地图服务
查看>>
TopN算法与排行榜
查看>>
lucene排序算法之向量空间模型(一)
查看>>
新浪微博数据Json格式解析
查看>>
WLAN 802.11 wifl区别
查看>>
oracle授权动态视图权限给用户
查看>>
Debian – 出现-bash: pip: command not found错误解决办法
查看>>
Zxing扫描二维码
查看>>
我的友情链接
查看>>
aspcms后台拿shell漏洞(非添加模块)及修复方法
查看>>
C语言冒泡排序法
查看>>
B2B行业门户网站群发邮件时间及发送频率
查看>>
关于虚拟机能ping通物理机,而物理机ping不通虚拟机问题解决。
查看>>
同台机器启动多个mysql
查看>>
iframe 跨域高度自适应
查看>>
struts2+hibernate3+spring3(ssh2)框架下的web应用
查看>>
Linux下的三个时间属性
查看>>