SQL 查看死锁情况的存储过程
| [日期:2012-06-05] |
来源:Linux社区
作者:yinxing |
[字体:大 中 小]
|
- 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

评论声明
- 尊重网上道德,遵守中华人民共和国的各项有关法律法规
- 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
- 本站管理人员有权保留或删除其管辖留言中的任意内容
- 本站有权在网站内转载或引用您的评论
- 参与本评论即表明您已经阅读并接受上述条款
|
|
|
|