分享

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

 mybook564 2014-04-12

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() 来自动产生行号
【责任编辑:赵鹏 TEL:(010)68476606】

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多