SQL2005查詢死鎖的表和具體的語句

查是哪一個進程死鎖了哪些表數據庫

select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName
from sys.dm_tran_locks where resource_type='OBJECT'session

 

 

下面的代碼是能夠看到死鎖的語句進程

use master
go
declare @spid int,@bl int
DECLARE s_cur CURSOR FOR
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
OPEN s_cur
FETCH NEXT FROM s_cur INTO @spid,@bl
WHILE @@FETCH_STATUS = 0
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 )
FETCH NEXT FROM s_cur INTO @spid,@bl
end
CLOSE s_cur
DEALLOCATE s_curci

相關文章
相關標籤/搜索