如果发生死锁了,我们怎么去检测具体发生死锁的是哪条SQL语句或存储过程?

这时我们可以使用以下存储过程来检测,就可以查出引起死锁的进程和SQL语句。SQL Server自带的系统存储过程sp_who和sp_lock也可以用来查找阻塞和死锁, 但没有这里介绍的方法好用。


检测死锁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
检测死锁

杀死锁和进程

如何去手动的杀死进程和锁?最简单的办法,重新启动服务。但是这里要介绍一个存储过程,通过显式的调用,可以杀死进程和锁。

检测死锁use master
检测死锁
go
检测死锁
检测死锁
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_killspid]'and OBJECTPROPERTY(id, N'IsProcedure'= 1)
检测死锁
drop procedure [dbo].[p_killspid]
检测死锁
GO
检测死锁
检测死锁
create proc p_killspid
检测死锁
@dbname varchar(200)    --要关闭进程的数据库名
检测死锁
as  
检测死锁    
declare @sql  nvarchar(500)  
检测死锁    
declare @spid nvarchar(20)
检测死锁
检测死锁    
declare #tb cursor for
检测死锁        
select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id(@dbname)
检测死锁    
open #tb
检测死锁    
fetch next from #tb into @spid
检测死锁    
while @@fetch_status=0
检测死锁    
begin  
检测死锁        
exec('kill '+@spid)
检测死锁        
fetch next from #tb into @spid
检测死锁    
end  
检测死锁    
close #tb
检测死锁    
deallocate #tb
检测死锁
go
检测死锁
检测死锁
--用法  
检测死锁
exec p_killspid  'newdbpy'
检测死锁


查看锁信息

如何查看系统中所有锁的详细信息?在企业管理管理器中,我们可以看到一些进程和锁的信息,这里介绍另外一种方法。

检测死锁--查看锁信息
检测死锁
create table #t(req_spid int,obj_name sysname)
检测死锁
检测死锁
declare @s nvarchar(4000)
检测死锁    ,
@rid int,@dbname sysname,@id int,@objname sysname
检测死锁
检测死锁
declare tb cursor for 
检测死锁    
select distinct req_spid,dbname=db_name(rsc_dbid),rsc_objid
检测死锁    
from master..syslockinfo where rsc_type in(4,5)
检测死锁
open tb
检测死锁
fetch next from tb into @rid,@dbname,@id
检测死锁
while @@fetch_status=0
检测死锁
begin
检测死锁    
set @s='select @objname=name from ['+@dbname+']..sysobjects where id=@id'
检测死锁    
exec sp_executesql @s,N'@objname sysname out,@id int',@objname out,@id
检测死锁    
insert into #t values(@rid,@objname)
检测死锁    
fetch next from tb into @rid,@dbname,@id
检测死锁
end
检测死锁
close tb
检测死锁
deallocate tb
检测死锁
检测死锁
select 进程id=a.req_spid
检测死锁    ,数据库
=db_name(rsc_dbid)
检测死锁    ,类型
=case rsc_type when 1 then 'NULL 资源(未使用)'
检测死锁        
when 2 then '数据库'
检测死锁        
when 3 then '文件'
检测死锁        
when 4 then '索引'
检测死锁        
when 5 then ''
检测死锁        
when 6 then ''
检测死锁        
when 7 then ''
检测死锁        
when 8 then '扩展盘区'
检测死锁        
when 9 then 'RID(行 ID)'
检测死锁        
when 10 then '应用程序'
检测死锁    
end
检测死锁    ,对象id
=rsc_objid
检测死锁    ,对象名
=b.obj_name
检测死锁    ,rsc_indid
检测死锁 
from master..syslockinfo a left join #t b on a.req_spid=b.req_spid
检测死锁
检测死锁
go
检测死锁
drop table #t

相关文章:

  • 2022-12-23
  • 2022-01-07
  • 2021-05-18
  • 2021-10-25
  • 2022-12-23
  • 2021-07-02
猜你喜欢
  • 2021-08-23
  • 2022-01-28
  • 2021-11-28
  • 2021-12-26
相关资源
相似解决方案