SQL 檢視被鎖表和解鎖

2021-08-27 14:12:53 字數 1348 閱讀 2044

檢視被鎖表:

select request_session_id spid,object_name(resource_associated_entity_id) tablename

from sys.dm_tran_locks

where resource_type='object' and object_name(resource_associated_entity_id) is not null

spid   鎖表程序 

tablename   被鎖表名

解鎖:

declare @spid  int 

set @spid  = 57 --鎖表程序

declare @sql varchar(50)

set @sql='kill '+cast(@spid  as varchar)

exec(@sql)

一次性解鎖多個程序占用的某張表

declare @tablename nvarchar(50)='ledger_zzjy_school';

if object_id('tempdb..#tempdb') is not null

begin

drop table #tempdb

end

select request_session_id spid,convert(nvarchar(50),object_name(resource_associated_entity_id)) tablename into #tempdb

from sys.dm_tran_locks

where resource_type='object';

declare @spid int,@sql varchar(50);

declare my_cursor cursor --定義游標

for (select distinct spid from #tempdb where tablename=@tablename) --查出需要的集合放到游標中

open my_cursor; --開啟游標

fetch next from my_cursor into @spid;

while @@fetch_status = 0

begin

set @sql='kill '+cast(@spid as varchar)

exec(@sql)

fetch next from my_cursor into @spid;

endclose my_cursor; --關閉游標

deallocate my_cursor; --釋放游標

oracle鎖表檢視被鎖的表和解鎖

以下幾個為相關表 select from v lock select from v sqlarea select from v session select from v process select from v locked object select from all objects sele...

oracle檢視被鎖的表和解鎖

檢視鎖表程序sql語句1 select sess.sid,sess.serial lo.oracle username,lo.os user name,ao.object name,lo.locked mode from v locked object lo,dba objects ao,v ses...

oracle檢視被鎖的表和解鎖

以下幾個為相關表 select from v lock select from v sqlarea select from v session select from v process select from v locked object select from all objects sele...