zoukankan      html  css  js  c++  java
  • SQL Server 查看数据库是否存在阻塞

     1 CREATE  procedure [dbo].[sp_who_lock]
     2 as
     3 begin
     4 declare @spid int,@bl int,
     5         @intTransactionCountOnEntry  int,
     6         @intRowcount    int,
     7         @intCountProperties   int,
     8         @intCounter    int
     9  create table #tmp_lock_who (id int identity(1,1),spid smallint,bl smallint)
    10  
    11  IF @@ERROR<>0 RETURN @@ERROR
    12  
    13  insert into #tmp_lock_who(spid,bl) select  0 ,blocked
    14    from (select * from master..sysprocesses where  blocked>0 ) a
    15    where not exists(select * from (select * from master..sysprocesses where  blocked>0 ) b
    16    where a.blocked=spid)
    17    union select spid,blocked from master..sysprocesses where  blocked>0
    18 
    19  IF @@ERROR<>0 RETURN @@ERROR
    20  
    21 -- 找到临时表的记录数
    22  select  @intCountProperties = Count(*),@intCounter = 1
    23  from #tmp_lock_who
    24  
    25  IF @@ERROR<>0 RETURN @@ERROR
    26  
    27  if @intCountProperties=0
    28   select '现在没有阻塞和死锁信息' as message
    29 
    30 -- 循环开始
    31 while @intCounter <= @intCountProperties
    32 begin
    33 -- 取第一条记录
    34   select  @spid = spid,@bl = bl
    35   from #tmp_lock_who where id = @intCounter
    36  begin
    37   if @spid =0
    38     select '引起数据库死锁的是: '+ CAST(@bl AS VARCHAR(10)) + '进程号,其执行的SQL语法如下'
    39  else
    40     select '进程号SPID:'+ CAST(@spid AS VARCHAR(10))+ '' + '进程号SPID:'+ CAST(@bl AS VARCHAR(10)) +'阻塞,其当前进程执行的SQL语法如下'
    41  DBCC INPUTBUFFER (@bl )
    42  end
    43 
    44 -- 循环指针下移
    45  set @intCounter = @intCounter + 1
    46 end
    47 
    48 
    49 drop table #tmp_lock_who
    50 
    51 return 0
    52 end
  • 相关阅读:
    Eclipse (indigo) 中安装jdk包并执行Maven
    UVA
    Android 仿QQ界面的实现
    Ajax是什么
    jieba.NET与Lucene.Net的集成
    jieba中文分词的.NET版本:jieba.NET
    SharePoint 2013技巧分享系列
    SharePoint 2013常用开发工具分享
    SharePoint 2013技巧分享系列
    SharePoint 2013技巧分享系列
  • 原文地址:https://www.cnblogs.com/howesdomo/p/5535626.html
Copyright © 2011-2022 走看看