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. EasyUI---tree

    EasyUI的tree在获取action返回的json字符串时最少具有三个属性id.text和children,这样在读取时才会在页面正常显示树形 这里比较重要的就是在数据库中对数据的存储吧,说白了还 ...

  2. Web API后端调用接口 (Get,POST,Put,Delete)

    using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...

  3. Openfire基础

    网上很多openfire相关资料,这里做下学习汇总 openfire官网:http://www.igniterealtime.org/ 可以下载openfire.spark.smack安装包及源码,安 ...

  4. jQuery.is() 函数

    is() 函数 判断当前对象是否符合指定表达式 语法 $selector.is(表达式)//指定表达式 返回值 返回值为布尔型(true/false) 当当前对象包含多个元素时,只要任意元素满足指定表 ...

  5. jQuery 查找父元素

    function deletesec1Div5(obj){ $(obj).closest(".sec1-div5").remove();}自己写的一段代码,实现了table中的全选 ...

  6. 慕课网__CSS_网页图标制作

    参考地址:http://www.flowerboys.cn/font/article/fontsArticle/how-to-turn-your-icons-into-a-web-font.html

  7. iOS 工程功能实现之好用的第三方

    1.http://www.cocoachina.com/ios/20140224/7868.html  (一个天气App案例) LBBlurredImage是一个继承自UIImageView,轻而易举 ...

  8. centos 忘记密码

    装了个  centos 6.8  安装的时候 要输入 新用户和密码 用 新的用户密码 进去后 各种没权限  重新修改 root 密码   一切OK 步骤 1.重新启动Centos,在启动过程中,长按“ ...

  9. 与String有关的强制转换

    String --> int int i = Integer.parseInteger("123"); String --> double double d = Dou ...

  10. SPSS数据分析—信度分析

    测量最常用的是使用问卷调查.信度分析主要就是分析问卷测量结果的稳定性,如果多次重复测量的结果都很接近,就可以认为测量的信度是高的.与信度相对应的概念是效度,效度是指测量值和真实值的接近程度.二者的区别 ...