UseMasterDeclare @spid int, @bl int, @intCounter int, @intRowcount int, @intCountProperties int, @intTransactionCountOnEntry int Declare @tmp_lock_who Table(IDint Identity(1,1), spidsmallint, BLsmallint) IF @ @ERROR<>0 SELECT @ @ERROR Insert into @tmp_lock_who(SPID,BL)Select 0, blocked from(Select * fromSys.sysprocesseswhereBlocked>0) Awhere not exists (Select * from(Select * fromSys.sysprocesseswhereBlocked>0) bwhereA.blocked=spid)Union SelectSpid,blocked fromSys.sysprocesseswhereBlocked>0 IF @ @ERROR<>0 SELECT @ @ERROR Select @intCountProperties = Count(*),@intCounter = 1 from @tmp_lock_who IF @ @ERROR<>0 SELECT @ @ERROR if @intCountProperties=0BEGIN SelectN'No blocking and deadlock information now' asmessageENDELSEbegin while @intCounter <= @intCountProperties begin Select @spid =spid@bl =BL from @tmp_lock_who whereId= @intCounter begin if @spid =0 SelectN'spid that caused the database deadlock:'+ CAST(@bl as VARCHAR(Ten))+N'SQL:' Else SelectN'SPID:'+ CAST(@spid as VARCHAR(Ten))+N'by spid:'+ CAST(@bl as VARCHAR(Ten))+N'blocking, SQL:' DBCCInputBuffer (@bl) End Set @intCounter = @intCounter + 1 EndEnd
Select request_session_id spid,object_name(resource_associated_entity_id) tableName from where resource_type='OBJECT'
SQL View Deadlock