SQL 鎖表 解鎖

2021-09-27 06:37:05 字數 2999 閱讀 5493

--鎖表(其它事務不能讀、更新、刪除)

begin tran

select * from 《表名》 with(tablockx);

waitfor delay '00:00:20'

commit tran

--鎖表(其它事務只能讀,不能更新、刪除)

begin tran

select * from 《表名》 with(holdlock);

waitfor delay '00:00:20'

commit tran

--鎖部分行

begin tran

select * from 《表名》 with(xlock) where id in ('81a2edf9-d1fd-4037-a17b-1369fd3b169b');

waitfor delay '00:01:20'

commit tran

-- 檢視鎖狀態

select spid,

blocked,

loginame,

last_batch,

status,

cmd,

hostname,

program_name

from sys.sysprocesses

where spid in (select blocked from sys.sysprocesses where blocked <> 0)

or (blocked <> 0)

-- 終止程序

declare @spid int

set @spid = 83 --鎖表程序

declare @sql varchar(1000)

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

exec(@sql)

---- 檢視語句-正在執行

select [spid] = session_id ,

ecid ,

[database] = db_name(sp.dbid) ,

[user] = nt_username ,

[status] = er.status ,

[wait] = wait_type ,

[individual query] = substring(qt.text,

er.statement_start_offset / 2,

( case when er.statement_end_offset = -1

then len(convert(nvarchar(max), qt.text))

* 2else er.statement_end_offset

end - er.statement_start_offset )

/ 2) ,

[parent query] = qt.text ,

program = program_name ,

hostname ,

nt_domain ,

start_time

from sys.dm_exec_requests er

inner join sys.sysprocesses sp on er.session_id = sp.spid

where session_id > 50 -- ignore system spids.

and session_id not in ( @@spid ) -- ignore this current statement.

order by 1 ,2

-- 檢視語句執行過資訊

select *

from sys.sysprocesses

where spid in (select blocked from sys.sysprocesses where blocked <> 0)

or (blocked <> 0) ;

select top 1000

st.text as '執行的sql語句',

qs.execution_count as '執行次數',

qs.total_elapsed_time as '耗時',

qs.total_logical_reads as '邏輯讀取次數',

qs.total_logical_writes as '邏輯寫入次數',

qs.total_physical_reads as '物理讀取次數',

qs.creation_time as '執行時間' ,

qs.*

from sys.dm_exec_query_stats qs

sys.dm_exec_sql_text(qs.sql_handle) st

where qs.creation_time between '2016-08-05 15:00:00' and '2016-08-05 17:00:00'

order by qs.total_elapsed_time desc

--kill當前庫所有程序

declare @sql nvarchar(max),@currdbname nvarchar(100)

set @currdbname='db1' --如果在當前庫上執行,可改為: set @currdbname = db_name()

while exists( select 1 from sys.sysprocesses where dbid=db_id(@currdbname) and spid!=@@spid )

begin

select top 1 @sql='kill '+cast(spid as nvarchar(50)) from sys.sysprocesses where dbid=db_id(@currdbname) and spid!=@@spid

exec (@sql)

end

Oracle 鎖表 解鎖

select l.session id sid,s.serial l.locked mode,l.oracle username,l.os user name,s.machine,s.terminal,o.object name,s.logon time from v locked object l...

鎖表解鎖語句

select alter system kill session lo.session id vss.serial killed dob.object name table name lo.session id vss.serial 刪除號 lo.locked mode lo.session id ...

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...