sql 查找死锁对象的存储过程
2024-08-26 02:58:26
USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_who_lock] Script Date: 05/12/2016 14:13:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO ALTER procedure [dbo].[sp_who_lock]
as
begin
--exec sp_who_lock 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
最新文章
- JavaScript中String对象的方法介绍
- (转)Java:类与继承
- 大熊君大话NodeJS之------MongoDB模块(额外篇)
- uva 1339
- lvs之ip-tun(ip隧道)技术的学习与实践
- hdu 3853LOOPS (概率DP)
- ELK:kibana使用的lucene查询语法
- 6个WordPress备份插件
- Spring+SpringMVC+Mybatis 利用AOP自定义注解实现可配置日志快照记录
- 关于sql、mysql语句的模糊查询分类与详解,包括基本用法和mapper.xml文件里插入写法
- linux自动化创建补丁
- Pytorch--Dropout笔记
- Swift Enum 枚举
- js 闭包 弊端
- 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
- delphi 启动停止windows服务 转
- Windows 10 修改系统环境变量后,CMD生效,Powershell未生效
- SQLSERVER的四个系统数据库
- POJ 1160 经典区间dp/四边形优化
- JS返回一个数据的千分位格式
热门文章
- JAVA-数组或集合
- CodeForces 785E Anton and Permutation 分块
- CodeForces 785C Anton and Fairy Tale 二分
- ceph 存储系统
- 滑动菜单栏之开源项目SlidingMenu的使用
- python之if测试
- Linux利用OneinStack搭建环境
- centos6系列问题
- 操作App.config的类(转载)
- linux 出错 “INFO: task xxxxxx: 634 blocked for more than 120 seconds.”的3种解决方案(转)