查看数据库里阻塞和死锁情况.sql

查看数据库里阻塞和死锁情况.sql

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]’) and OBJECTPROPERTY(id, N’IsProcedure’) = 1)

drop procedure [dbo].[sp_who_lock]

GO

/***************************************************************************

//  创建 : fengyu  邮件 : maggiefengyu@tom.com  日期 :2004-04-30

//  修改 : 从http://www.csdn.net/develop/Read_Article.asp?id=26566学习到并改写 

//  说明 : 查看数据库里阻塞和死锁情况

***************************************************************************/

use master

go

create procedure sp_who_lock

as

begin

declare @spid int,@bl int,

 @intTransactionCountOnEntry  int,

        @intRowcount    int,

        @intCountProperties   int,

        @intCounter    int

 create table #tmp_lock_who (

 id int identity(1,1),

 spid smallint,

 bl smallint)

 

 IF @@ERROR<>0 RETURN @@ERROR

 

 insert into #tmp_lock_who(spid,bl) select  0 ,blocked

   from (select * from sysprocesses where  blocked>0 ) a

   where not exists(select * from (select * from sysprocesses where  blocked>0 ) b

   where a.blocked=spid)

   union select spid,blocked from sysprocesses where  blocked>0

 IF @@ERROR<>0 RETURN @@ERROR

 

— 找到临时表的记录数

 select  @intCountProperties = Count(*),@intCounter = 1

 from #tmp_lock_who

 

 IF @@ERROR<>0 RETURN @@ERROR

 

 if @intCountProperties=0

  select ‘现在没有阻塞和死锁信息’ as message

— 循环开始

while @intCounter <= @intCountProperties

begin

— 取第一条记录

  select  @spid = spid,@bl = bl

  from #tmp_lock_who where Id = @intCounter

 begin

  if @spid =0

            select ‘引起数据库死锁的是: ‘+ CAST(@bl AS VARCHAR(10)) + ‘进程号,其执行的SQL语法如下’

 else

            select ‘进程号SPID:’+ CAST(@spid AS VARCHAR(10))+ ‘被’ + ‘进程号SPID:’+ CAST(@bl AS VARCHAR(10)) +’阻塞,其当前进程执行的SQL语法如下’

 DBCC INPUTBUFFER (@bl )

 end

— 循环指针下移

 set @intCounter = @intCounter + 1

end

drop table #tmp_lock_who

return 0

end

exec sp_who_lock

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100639.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 菜鸟的mybatis实战教程

    菜鸟的mybatis实战教程菜鸟的mybatis实战教程说明一、mybatis简介二、mybatis简单入门(1)数据库创建(2)创建springboot项目(3)增删改查操作(4)总结说明之前学习了mybatis之后没有做记录,在前几天做一个题库系统时,刚好要用到mybatis,因为之前做东西时用的jpa,mybatis没怎么去用,导致有些知识点想不起来,在使用mybatis出现了一些低级的错误,现将mybatis的学习笔记记录在这里,以便日后查看,本文会持续更新,不断地扩充本文仅为记录学习轨迹,如有侵权,联系删除一、myb

    2022年5月16日
    41
  • 江西公安网_南昌汽车网

    江西公安网_南昌汽车网程序介绍:江西爱车网——地方汽车门户网站程序采用ASP+ACCESS开发,前台设计美观大方,带有会员中心,会员类型分为:个人、经销商及4S店、二手车商及经纪人、其他经销商等,网站频道设有:买车、卖车、租车、用车、车市、车友、车界,还带有车友论坛及供求信息发布功能。 百度网盘下载http://pan.baidu.com/netdisk/singlepublic?fid=372892_1051

    2022年10月1日
    6
  • git每次push和pull都要输入密码

    git每次push和pull都要输入密码

    2022年2月18日
    40
  • paip.提高工作效率–数据绑定到table原则和过程Angular js jquery实现

    paip.提高工作效率–数据绑定到table原则和过程Angular js jquery实现

    2022年1月3日
    55
  • 哪个游戏盒子里有JAVA_关于点和盒子游戏的Java minimax

    哪个游戏盒子里有JAVA_关于点和盒子游戏的Java minimax我想建议您完全重新考虑代码.查看代码的问题(以及为什么这里没有很多响应)是很难遵循并且很难调试.例如,什么是gs.getRemainingLines,它究竟做了什么?(为什么剩下的线而不是所有合法的线?)但是,通过一些简化,可以更容易地弄清楚发生了什么并修复它.在抽象层面,minimax只是这个过程:floatminimax_max(GameStateg){if(gisterminal…

    2022年7月7日
    26
  • Two Days PowerShell:3

    Two Days PowerShell:3

    2021年8月5日
    65

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号