SQL Serverのデッドロックのビューテーブルとプロセスのデッドロックを殺します

チェックアウト

選択   
    request_session_id SPID、  
    OBJECT_NAME(resource_associated_entity_id)tableNameのを   
から   
    sys.dm_tran_locks       RESOURCE_TYPE = 'OBJECT 

プロセスのデッドロックを殺します

SPIDを殺します 

 

 

もう一つ:

EXEC master.dbo.sp_who_lock - 現在のプロセスのデッドロックを表示します

幹部は、原因となったプロセスのデッドロックを殺すytsafety-- master.dbo.p_killspid

 

次のようにsp_who_lock:

ONのSET QUOTED_IDENTIFIER
GO
ON SET ANSI_NULLS
GO


手続きsp_who_lockを作成する
よう
開始
宣言@spidのint、int型@ BL、
@intTransactionCountOnEntry int型、
        @intRowcount int型、
        @intCountProperties int型、
        @intCounter int型

表#tmp_lock_who(登録
IDのint型のアイデンティティ(1,1)、
SPID SMALLINT、
BL SMALLINT)を

IF @@ ERROR <> 0 RETURN @@ ERROR

(SPID、BL)#tmp_lock_who挿入0を選択し、ブロックされた
   から(ブロックsysprocessesのSELECT * FROM> 0)、A
   ((> 0をブロックsysprocessesのSELECT * FROM)B SELECT * FROM存在しない
   a.blocked = SPID)
   > 0をブロックされたsysprocessesのを阻止組合選択のspid、

@@ ERROR IF <> 0リターンエラー@@ -レコード数の一時テーブルを検索するSELECT =の@intCountPropertiesにCOUNT(*),. 1 @ = intCounterを#tmp_lock_whoから
 


IF @@ ERROR <> 0 RETURN @@ ERROR

@ intCountProperties = 0 IF
  SELECT「なしブロックと情報デッドロック」というメッセージなどを

-- 循环开始
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

 

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

 

p_killspid 如下:

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
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
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

おすすめ

転載: www.cnblogs.com/CandiceW/p/10984030.html