1. ホーム
  2. データベース
  3. mssql2005

SQL2005 ビューデッドロック ストアドプロシージャ sp_who_lock

2022-01-09 21:14:30

以下は、デッドロック、ブロックSQL文のパフォーマンステストの過程で、監視のSQLサーバのデータベースの私のコンパイルは、まだ比較的準備ができている、戻って滞在。

コールメソッド:対応するデータベースを選択し、実行するexec sp_who_lockを

USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE procedure [dbo]. [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 
 
-- Find the number of records in the temporary table 
select @intCountProperties = Count(*),@intCounter = 1 
from #tmp_lock_who 

IF @@ERROR<>0 RETURN @@ERROR 

if @intCountProperties=0 
	select 'No blocking and deadlock messages now' as message 

-- Loop start 
while @intCounter <= @intCountProperties 
begin 
	-- take the first row 
	select @spid = spid,@bl = bl 
	from #tmp_lock_who where Id = @intCounter 
	begin 
		if @spid =0 
      select 'The database deadlock is caused by: ' + CAST(@bl AS VARCHAR(10)) + 'Process number, the SQL syntax is as follows' 
		else 
      select 'Process number SPID: '+ CAST(@spid AS VARCHAR(10)) + 'Blocked by' + 'Process number SPID: '+ CAST(@bl AS VARCHAR(10)) + 'The syntax of the SQL executed by the current process is as follows' 
		DBCC INPUTBUFFER (@bl ) 
	end 

	-- move the loop pointer down 
	set @intCounter = @intCounter + 1 
end 

drop table #tmp_lock_who 

return 0 
end