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