if exists(Select * fromDbo.sysobjectswhereId= object_id(N'[dbo]. [Sp_who_lock]') and ObjectProperty(ID, N'isprocedure')= 1)Drop procedure [dbo].[Sp_who_lock]GO UseMasterGoCreate procedureSp_who_lock asbeginDeclare @spid int,@bl int, @intTransactionCountOnEntry int, @intRowcount int, @intCountProperties int, @intCounter int Create Table#tmp_lock_who (IDint Identity(1,1), spidsmallint, BLsmallint) IF @ @ERROR<>0 RETURN @ @ERROR Insert into#tmp_lock_who (SPID,BL)Select 0, blocked from(Select * fromsysprocesseswhereBlocked>0) Awhere not exists(Select * from(Select * fromsysprocesseswhereBlocked>0) bwhereA.blocked=spid)Union SelectSpid,blocked fromsysprocesseswhereBlocked>0 IF @ @ERROR<>0 RETURN @ @ERROR --number of records found for temporary table Select @intCountProperties = Count(*),@intCounter = 1 from#tmp_lock_whoIF @ @ERROR<>0 RETURN @ @ERROR if @intCountProperties=0 Select 'No blocking and deadlock information now' asmessage--Cycle Start while @intCounter <= @intCountPropertiesbegin--take the first record Select @spid =spid@bl =BL from#tmp_lock_whowhereId= @intCounter begin if @spid =0 Select 'causing the database deadlock is:'+ CAST(@bl as VARCHAR(Ten))+ 'process number, which executes the following SQL syntax' Else Select 'process number spid:'+ CAST(@spid as VARCHAR(Ten))+ 'was' + 'process number spid:'+ CAST(@bl as VARCHAR(Ten))+'The SQL syntax that the current process executes is blocked as follows' DBCCInputBuffer (@bl ) End --The loop pointer moves down Set @intCounter = @intCounter + 1EndDrop Table#tmp_lock_whoreturn 0End
SQL Server View deadlock stored Procedures (reproduced)