链接 :http://www.cnblogs.com/mzhanker/archive/2011/06/04/2072739.html

使用sqlserver作为数据库的应用系统,都避免不了有时候会产生死锁, 死锁出现以后,维护人员或者开发人员大多只会通过sp_who来查找死锁的进程,然后用sp_kill杀掉。利用sp_who_lock这个存储过程,可 以很方便的知道哪个进程出现了死锁,出现死锁的问题在哪里.

创建sp_who_lock存储过程

CREATE procedure sp_who_lock  
as     
begin     
   declare @spid int     
   declare @blk int     
   declare @count int     
   declare @index int     
   declare @lock tinyint     
   set @lock=0     
   create table #temp_who_lock     
 (     
  id int identity(1,1),     
  spid int,     
  blk int     
 )     
 if @@error<>0 return @@error     
 insert into #temp_who_lock(spid,blk)     
 select 0 ,blocked      
 from (select * from master..sysprocesses where blocked>0)a     
 where not exists(select * from  master..sysprocesses where a.blocked =spid and blocked>0)     
 union select spid,blocked from  master..sysprocesses where blocked>0     
 if @@error<>0 return @@error     
 select @count=count(*),@index=1 from #temp_who_lock     
 if @@error<>0 return @@error     
 if @count=0     
 begin     
  select '没有阻塞和死锁信息'     
  return 0     
 end     
 while @index<=@count      
 begin     
  if exists(select 1 from #temp_who_lock a where id>@index and exists(select 1 from #temp_who_lock where id<=@index and a.blk=spid))     
  begin     
   set @lock=1     
   select @spid=spid,@blk=blk from #temp_who_lock where id=@index     
   select '引起数据库死锁的是: '+ CAST(@spid AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'     
   select  @spid, @blk   
   dbcc inputbuffer(@spid)     
   dbcc inputbuffer(@blk)     
  end     
  set @index=@index+1     
 end     
 if @lock=0      
 begin     
  set @index=1     
  while @index<=@count      
  begin     
   select @spid=spid,@blk=blk from #temp_who_lock where id=@index     
   if @spid=0     
    select '引起阻塞的是:'+cast(@blk as varchar(10))+ '进程号,其执行的SQL语法如下'     
   else      
    select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '被' + '进程号SPID:'+ CAST(@blk AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'     
   dbcc inputbuffer(@spid)   
   dbcc inputbuffer(@blk)     
   set @index=@index+1     
  end     
 end     
 drop table #temp_who_lock     
 return 0     
end           
 
 
GO

在查询分析器中执行:

exec sp_who_lock

直到最后的结果为:

 

查询Sqlserver数据库死锁的一个存储过程(转)的更多相关文章

  1. 查询Sqlserver数据库死锁的一个存储过程(转)

        使用sqlserver作为数据库的应用系统,都避免不了有时候会产生死锁, 死锁出现以后,维护人员或者开发人员大多只会通过sp_who来查找死锁的进程,然后用sp_kill杀掉.利用sp_who ...

  2. 查询Sqlserver数据库死锁的一个存储过程

    From:http://www.cnblogs.com/mzhanker/archive/2011/06/04/2072739.html 使用sqlserver作为数据库的应用系统,都避免不了有时候会 ...

  3. 压缩SQLServer数据库日志的一个存储过程

    use master --注意,此存储过程要建在master数据库中 go if exists (select * from dbo.sysobjects where id = object_id(N ...

  4. jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上

    jsp中使用Servlet查询SQLSERVER数据库中的表的信息,并且打印在屏幕上 1.JavaBean的使用 package com.zheng; public class BookBean { ...

  5. SQLSERVER数据库死锁与优化杂谈

    死锁杂谈 当数据库死锁时,SqlServer会释放一个优先级较低的锁,让另一个事务运行:所以,即时去捕捉数据库死锁,是挺不容易的. 如果,数据库死锁比较长时间,那么死锁是可以被捕捉的. 可以用SqlS ...

  6. Python查询SQLserver数据库备份(抛砖引玉)

    通过python pymssql直接访问SQLserver数据库,查找其数据库mode,这个脚本具有很强的抛砖引玉特性: 1.可以巡检多台多数据库服务器 2.query内容可以多样化,譬如查询死锁.连 ...

  7. JavaWeb连接SQLServer数据库并完成一个登录界面及其功能设计。

    一.JDBC连接SQLserver数据库的步骤: 1.下载SQLserver的JDBC驱动文件——Microsoft JDBC Driver 4.0 for SQL Server 2.例如下载得到的文 ...

  8. Go语言中查询SqlServer数据库

    一.Go语言中查询MsSQL数据库: // main.go package main import ( "database/sql" "fmt" "l ...

  9. NHibernate中,查询SqlServer数据库多个实体对象

    关于datetime类型使用:  Oracle:  "and tb.EffectiveDate >= to_date(?,'yyyy-mm')" Sql:  "an ...

随机推荐

  1. JS常用的三种匿名函数

    第一种: var f1=function(p1,p2){ return p1+p2; };//将函数赋值给一个变量 alert(f1(1,3)); 匿名函数没法调用,只能赋值给一个变量,由于是赋值语句 ...

  2. 使用NPOI将多张图片导入execl

    protected void btn_Export_Click(object sender, EventArgs e) { List<BNXX_SJXJ_XJSJ> list = View ...

  3. Engine中执行gp工具返回的要素图层如何获取?

    来自:http://zhihu.esrichina.com.cn/?/question/12087 Engine中执行gp工具返回的[解决办法]:需要用gpUtils.DecodeFeatureLay ...

  4. 解析plist文件(字典里包着数组,数组中又包含字典)

    #import "RootTableViewController.h" #import "City.h" @interface RootTableViewCon ...

  5. 安卓第十一天笔记-Intent与inter-filter配置

    安卓第十一天笔记-Intent与inter-filter配置 Intent与inter-filter配置 1.Intent对象简述 Android应用中有包含三种重要组件:Activity,Servi ...

  6. Struts2(十)OGNL标签二与Struts2标签

    一.Struts2标签的优势 标签库简化了用户对标签的使用 结合OGNL使用,对于集合.对象的访问功能非常强大 提供可扩展的主题.模板支持.极大简化了视图页面的编写 不依赖任何表现层技术 Struts ...

  7. 27个提升效率的iOS开源库推荐

    DZNEmptyDataSet(UI,空表格视图解算器) PDTSimpleCalendar(UI,drop-in日历组件) MagicalRecord(实施活跃记录模式的Core Data助手) C ...

  8. 手动删除webapps下项目,导致Document base %TOMCAT_HOME%\webapps\XXX does not exist or is not a readable directory

    删除 %TOMCAT_HOME%\conf\XXX.xml , 再次eclipse中重新启动tomcat,错误就会消失.

  9. Visual Studio发布Web项目报错:Unable to add 'xxx' to the Web site. Unable to add file 'xxx'. The specified file could not be encrypted.

    背景 Visual Studio下的Web项目 现象 发布时遇到Unable to add 'xxx' to the Web site.  Unable to add file 'xxx'. The ...

  10. sql 把特定数据排在最前面

    感谢www.baidu.com/p/dongfanghong_1 sql大神,简单的语法运用起来简直活了. 第一法] select * from table where name='D' UNION ...