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
原文地址:https://www.cnblogs.com/howesdomo/p/5535626.html