sql-server – 如何查找锁定的表名(特定于任何事务)

有没有办法列出锁定的表并杀死交易,如果我们希望它们立即解锁.

或者我们需要遵循的任何其他术语,我正在寻找上述操作.

任何帮助或指导将不胜感激.

解决方法

这将使用 sys.dm_tran_locks DMV显示所有具有独占锁定的数据库(可能包括在运行时保留的瞬态锁定):
select d.*,l.* from sys.dm_tran_locks l
join sys.databases d on l.resource_database_id = d.database_id 
where l.request_mode = 'X'

(X =独占,S =共享,IS =意图共享)见Lock Modes.

但可能最好的方法是打开Trace Flags 1204和1222:

Trace Flag 1204 and Trace Flag 1222
When deadlocks occur,trace flag 1204
and trace flag 1222 return information
that is captured in the sql Server
2005 error log. Trace flag 1204
reports deadlock information formatted
by each node involved in the deadlock.
Trace flag 1222 formats deadlock
information,first by processes and
then by resources. It is possible to
enable both trace flags to obtain two
representations of the same deadlock
event.

参考:Detecting and Ending Deadlocks

另外,运行sp_who2并查找BlkBy(Blocked By)列中的条目;按照这些,直到你到达死锁链的头部.这是负责的进程标识符(或PID).

获取在特定进程后面运行的sql,您可以运行:

dbcc inputbuffer (@pid)

并使用该PID来杀死进程(谨慎并且风险自负):

kill @pid

查看Who is Active? v10.00: DMV Monitoring Made Easy

另请阅读Blocking is not Deadlocking(区分两种情况)

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...