码迷,mamicode.com
首页 > 数据库 > 详细

SQL 查看 死锁

时间:2015-08-28 12:26:42      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

use master
declare @spid int,
        @bl int,        
        @intCounter    int,
        @intRowcount    int,
        @intCountProperties   int,
        @intTransactionCountOnEntry  int

 declare  @tmp_lock_who table (
          id int identity(1,1),
          spid smallint,
          bl smallint)
 
 IF @@ERROR<>0 
    SELECT @@ERROR
 
 insert into @tmp_lock_who(spid,bl) select  0 ,blocked
   from (select * from SYS.sysprocesses where  blocked>0 ) a 
   where not exists
        (select * from (select * from SYS.sysprocesses where  blocked>0 ) b 
   where a.blocked=spid)
   union select spid,blocked from SYS.sysprocesses where  blocked>0

 IF @@ERROR<>0 
SELECT @@ERROR 

 select  @intCountProperties = Count(*),@intCounter = 1
 from @tmp_lock_who
 
 IF @@ERROR<>0 
SELECT @@ERROR 
 
 if @intCountProperties=0
BEGIN
  select N现在没有阻塞和死锁信息 as message
END
ELSE
begin
 while @intCounter <= @intCountProperties
 begin
  select  @spid = spid,@bl = bl
  from @tmp_lock_who where Id = @intCounter 
  begin
  if @spid =0 
      select N引起数据库死锁的SPID: + CAST(@bl AS VARCHAR(10)) + N SQL: 
   else
      select NSPID: + CAST(@spid AS VARCHAR(10))+ N被SPID: + CAST(@bl AS VARCHAR(10)) + N阻塞,SQL: 
  DBCC INPUTBUFFER (@bl)
  end
 set @intCounter = @intCounter + 1
 end
end

 

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

 

SQL 查看 死锁

标签:

原文地址:http://www.cnblogs.com/ilookbo/p/4765856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!