From: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数据库死锁的一个存储过程(转)

    链接 :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. 我为开源做贡献,网页正文提取——Html2Article

    为什么要做正文提取 一般做舆情分析,都会涉及到网页正文内容提取.对于分析而言,有价值的信息是正文部分,大多数情况下,为了便于分析,需要将网页中和正文不相干的部分给剔除.可以说正文提取的好坏,直接影响了 ...

  2. Windows安装Scrapy遇坑解决办

    PS: Windows真心不适合开发.且行且珍惜.... 坑: error: Setup script exited with error: Microsoft Visual C++ 9.0 is r ...

  3. HTML5滑动(swipe)事件

    移动H5开发中经常用到滑动效果(页面上移.下移.向左滑动.向右滑动等),浏览器并没有内置swipe事件,可以通过touch事件(touchstart.touchmove和touchend)模拟swip ...

  4. .net比较完美的动态注册com组件

    .net中经常需要使用com组件,怎么样注册com组件呢? 一般想到的当然是直接通过系统cmd 调用regsvr32注册程序去注册,如下: regsvr32 name.dll 在.net中可以直接执行 ...

  5. Java知识积累-XML的DOM解析修改和删除方法

    import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder;import jav ...

  6. (原创)vim配色------水果色,不伤眼。

  7. Linux 利用 locate 和 find 查找文件

    Linux 利用 locate 和 find 查找文件 命令 locate 用于快速查找文件.文件夹.此命令并没有在磁盘上查找所有文件,而是在预先建立的数据库里进行搜索.可以使用 updatedb 命 ...

  8. eslintrc配置翻译

    { "env": { "browser": true, "node": true, "commonjs": true } ...

  9. 前台jquery+ajax+json传值,后台处理完后返回json字符串,如何取里面的属性值?(不用springmvc注解)

    一.取属性值 前台页面: function select(id){ alert("hfdfhdfh"+id); $.ajax({ url:"selectByid.jsp& ...

  10. Matrix

    记载: Matrix Matrix是Android 提供的一个矩阵工具类,位于"android.graphics.Matrix"包下,它本身不能对图像或View进行变换, 但它可以 ...