博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
查看死锁存储过程(转载)
阅读量:6898 次
发布时间:2019-06-27

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

查看死锁存储过程 

Posted on 2008-10-17 10:06 西北的荒漠 阅读(12) 评论(0)  编辑 收藏 网摘  

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

本文转自温景良博客园博客,原文链接:http://www.cnblogs.com/wenjl520/archive/2008/10/23/1317731.html,如需转载请自行联系原作者

你可能感兴趣的文章
软路由 - 开篇
查看>>
mac下Fiddler的安装-启动
查看>>
maven 插件
查看>>
java泛型学习3之类型参数的限制
查看>>
Oracle 多表连接
查看>>
技术分享连载(二十一)
查看>>
mongodb3.x版本用户管理方法
查看>>
配置pacemaker时用到的一些CRM CLI命令
查看>>
RMAN 测试脚本
查看>>
精彩 .NET 2015
查看>>
C# 温故知新 基础篇(11) 泛型<思维导图>
查看>>
include file 与include virtual的区别
查看>>
思維的枷鎖
查看>>
Python天天美味(29) - 调用VC++的动态链接库(DLL)
查看>>
Prim 最小生成树算法
查看>>
金蝶结账的时候出现压缩账套失败 KIC_Compress.KD
查看>>
JavaScriptSerializer 序列化json 时间格式
查看>>
PyCharm 总结
查看>>
Microsoft Enterprise Library 5.0 系列教程(九) Policy Injection Application Block
查看>>
ASP.NET Core 返回 Json DateTime 格式
查看>>