Sql server 锁表查询Sql

查询SQLServer表被锁的类型

declare @dbname nvarchar(200),@objid nvarchar(200)
 
set @dbname='PETTY_CASH_ORIGIN'
set @objid='PC_INVOICE_MASTER'
 
Select a.req_spid,db_name(a.rsc_dbid) as 數據庫,object_name(a.rsc_objid) as 表,
資源類型=case a.rsc_type when 1 then 'NULL 資源(未使用)'
   when 2 then '數據庫'
   when 3 then '文件'
   when 4 then '索引'
   when 5 then '表'
   when 6 then '頁'
   when 7 then '鍵'
   when 8 then '擴展盤區'
   when 9 then 'RID(行 ID)' 
   when 10 then '應用程序'
   end,
鎖模式=case a.req_mode when 0 then 'Null'
 when 1 then 'Sch-S(架构性定性)'
 when 2 then 'Sch-M(架构修改)'
 when 3 then 'S(共享鎖)' 
 when 4 then 'U更新'
 when 5 then 'X排它鎖'
 when 6 then 'IS意向共享'
 when 7 then ''
 end,
鎖狀態=case a.req_status when 1 then '已授權'
              when 2 then '正在轉換'
 when 3 then '正在等待'
 end,
對象類型=case a.req_ownertype when 1 then '事務'
 when 2 then '遊標'
 when 3 then '會話'
 end
 from master.dbo.syslockinfo as a,master.dbo.sysprocesses as b
where a.rsc_dbid = b.dbid AND a.req_spid = b.spid 
AND a.rsc_type=5 and object_name(a.rsc_objid)=@objid and db_name(a.rsc_dbid)=@dbname
order by req_spid

1,查看那个表死锁

select object_name(resource_associated_entity_id) as tableName, request_session_id as pid from sys.dm_tran_locks
where resource_type = 'OBJECT'

2,结束死锁的进程

kill 70
posted @ 2020-08-03 11:58  颖子的踏坑路线  阅读(609)  评论(0编辑  收藏  举报