zoukankan      html  css  js  c++  java
  • sql 查找死锁对象的存储过程

     1 USE [master]
     2 GO
     3 /****** Object:  StoredProcedure [dbo].[sp_who_lock]    Script Date: 05/12/2016 14:13:46 ******/
     4 SET ANSI_NULLS ON
     5 GO
     6 SET QUOTED_IDENTIFIER ON
     7 GO
     8 
     9 
    10 ALTER procedure [dbo].[sp_who_lock] 
    11 as 
    12 begin 
    13     --exec sp_who_lock
    14     
    15 declare @spid int, @bl int, @intTransactionCountOnEntry int, @intRowcount int, @intCountProperties int, @intCounter int 
    16 create table #tmp_lock_who ( 
    17  id int identity(1,1), 
    18  spid smallint, 
    19  bl smallint
    20 ) 
    21 IF @@ERROR<>0 RETURN @@ERROR 
    22 insert into #tmp_lock_who(spid,bl) select 0 ,blocked 
    23  from (select * from sysprocesses where blocked>0 ) a 
    24  where not exists(select * from (select * from sysprocesses where blocked>0 ) b 
    25  where a.blocked=spid) 
    26  union select spid,blocked from sysprocesses where blocked>0 
    27 IF @@ERROR<>0 RETURN @@ERROR 
    28 
    29 -- 找到临时表的记录数 
    30 select @intCountProperties = Count(*),@intCounter = 1 
    31 from #tmp_lock_who 
    32 IF @@ERROR<>0 RETURN @@ERROR 
    33 if @intCountProperties=0 
    34  select '现在没有阻塞和死锁信息' as message 
    35 -- 循环开始 
    36 while @intCounter <= @intCountProperties 
    37 begin 
    38 -- 取第一条记录 
    39 select @spid = spid,@bl = bl 
    40  from #tmp_lock_who where Id = @intCounter 
    41  begin 
    42  if @spid =0 
    43  select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下' 
    44 else 
    45  select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下' 
    46 DBCC INPUTBUFFER (@bl ) 
    47  end 
    48 -- 循环指针下移 
    49 set @intCounter = @intCounter + 1 
    50 end 
    51 drop table #tmp_lock_who 
    52 return 0 
    53 end
  • 相关阅读:
    ||和&&
    用jQuery编的一个分页小代码
    Intent携带额外的数据的方法
    Handler消息传递机制
    安卓中的消息提示
    使用AlertDialog创建对话框的大致步骤
    布局管理器
    Android中支持的常用距离单位
    开发自定义View
    Gridview中奇偶数行颜色设置
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/5485503.html
Copyright © 2011-2022 走看看