27,579
社区成员
发帖
与我相关
我的任务
分享
-- 查看表上设置的锁(SQL Server 2005)
SELECT resource_type, resource_associated_entity_id,
(case when resource_type='OBJECT' then object_name(resource_associated_entity_id)
else (select object_name([object_id]) from sys.partitions where l.resource_associated_entity_id=hobt_id) end) tabname,
request_status, request_mode,request_session_id,resource_description
FROM sys.dm_tran_locks l where resource_type in ('OBJECT','RID','PAGE','KEY')
/*
功 能:查看sqlserver的表 锁定情况(用户、进程、表)
作 者:金振水
日 期:2009-12-15
备 注:在sqlserver2000上调试通过;
所查找的对象 到数据库的基本 objects 级别
欢迎修改完善!
*/
declare @dbname sysname
set @dbname = 'usercenter'
declare @objname sysname
set @objname = 'tp_uidinfo'
declare @exec varchar(500)
--准备进程快照
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
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)
--准备用户信息快照
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