SQL Server 2008查看死锁的存储过程的代码示例

数据库 SQL Server
本文我们主要介绍了SQL Server 2008查看死锁的存储过程的代码示例,希望能够对您有所帮助。

SQL Server 2008数据库中,查看死锁可以用存储过程来实现,本文我们主要就介绍了SQL Server 2008查看死锁的存储过程的代码示例,希望能够对您有所帮助。

代码示例如下:

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

 

以上就是SQL Server 2008查看死锁的存储过程的代码示例的全部内容,本文我们就介绍到这里了,希望本次的介绍能够对您有所收获!

【编辑推荐】

  1. SQL Server 2005导入Oracle 10g的C#源码
  2. SQL Server 2008快速清理日志文件的代码
  3. SQL Server 2008数据库中CDC的功能使用及说明
  4. SQL Server 2008阻止保存要求重新创建表的更改的问题
  5. SQL Server数据库row_number() over() 来自动产生行号
责任编辑:赵鹏 来源: CSDN博客
相关推荐

2011-08-22 10:15:39

数据库存储过程

2011-09-01 15:24:22

SQL Server 存储过程调试

2011-08-16 16:59:58

PLSQL分页存储过程Java

2010-07-15 12:38:14

SQL Server存

2011-03-24 13:38:47

SQL Server 存储分页

2011-08-12 14:01:21

SQL Server存储过程

2011-08-11 09:49:33

SQL Server 存储过程插入更新数据

2011-09-01 15:40:42

SQL Server存储过程和存储函数的加

2011-09-01 13:09:58

SQL Server DataTable作为

2011-09-01 14:00:11

SQL Server 存储过程显示表结构

2010-07-06 17:09:45

SQL Server索

2010-11-10 13:03:15

SQL Server存

2010-02-06 09:43:52

SQL Server

2010-07-13 10:35:20

SQL Server2

2011-02-28 15:46:22

SQLSQL ServerSQL Server

2009-08-06 16:44:06

2010-04-12 09:26:52

Oracle查询分页

2010-06-30 14:36:49

SQL Server

2010-09-14 10:47:45

sql server存

2011-03-31 10:38:28

SQL Server编写优化
点赞
收藏

51CTO技术栈公众号