Database Lock

use AX2009DEV
go



declare @dbname sysname
    set @dbname = 'AX2009DEV'
declare @objname sysname
    set @objname = 'tp_uidinfo'
declare @exec varchar(500)

--准备进程快照
drop table #proc
create table #proc(spid int,dbid int,objid bigint,indid bigint, [type] varchar(10),resource varchar(30), mode varchar(10),status varchar(10))
insert into #proc exec sp_lock 

drop table #t1
create table #t1(dbid int,dbname sysname ,objid bigint,obj_name sysname,obj_type varchar(20))
set @exec = 'select db_id(''' + @dbname + ''') as dbid,''' + @dbname +  '''as dbname,id,name,type from ' + @dbname + '.dbo.sysobjects where name like ''%' + @objname + '%'''
insert into #t1 execute(@exec)


--准备用户信息快照
drop table #who
create table #who(spid int,ecid int,status varchar(20),loginame varchar(256),hostname varchar(256), blk int,dbname varchar(50),cmd varchar(32))
insert into #who exec sp_who


--查询锁资源信息(注意引起死锁的进程spid)
select a.spid,c.loginame,c.hostname,c.blk as deaded_spid,c.cmd,c.status as user_status,a.dbid,b.dbname,a.objid,b.obj_name,b.obj_type,a.indid,a.type,a.resource,a.mode,a.status
from #proc a inner join #t1 b on a.dbid = b.dbid and a.objid = b.objid inner join #who c on a.spid=c.spid
where type in ('TAB') --可以注释掉以获取更详细的资料
order by a.spid,blk



--清理临时表
drop table #t1
drop table #proc
drop table #who

--sp_who

 

 

  1: sp_who 或 sp_who2  ,sp_lock  
  2: Select * from sysprocesses where blocked <> 0  
  3: 企业管理器->服务器->管理工具->活动->当前活动 然后把他kill掉。。。  
  4:SQL事件探查器,监控一下,看主要是那些处理引起的死锁.然后做相应的处理.  
  用事件探查器new一个trace,监视一下造成你sqlserver停顿的情况。。。  
   
  最好的办法还是检查一下引起锁的原因,一般是由你的代码引起的

 


sp_who

sp_who2

sp_lock

kill 进程ID(如51)

 

create  procedure 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 
  
-- 找到临时表的记录数
 select  @intCountProperties = Count(*),@intCounter = 1
 from #tmp_lock_who
 
 IF @@ERROR<>0 RETURN @@ERROR 
 
 if @intCountProperties=0
  select '\现在没有阻塞和死锁信息\' as message

-- 循环开始
while @intCounter <= @intCountProperties
begin
-- 取第一条记录
  select  @spid = spid,@bl = bl
  from #tmp_lock_who where Id = @intCounter 
 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 )
 end 

-- 循环指针下移
 set @intCounter = @intCounter + 1
end

drop table #tmp_lock_who

return 0
end   

GO

posted @ 2011-01-06 16:18  Fandy Xie  Views(511)  Comments(0Edit  收藏  举报