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
查询死锁对象
查询死锁对象
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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-27
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2021-10-28
  • 2022-12-23
  • 2021-12-10
  • 2021-12-10
  • 2022-01-31
  • 2021-08-21
相关资源
相似解决方案