有关数据库锁表

--===========
--===========查看被锁表语句
SELECT  request_session_id spid ,
        OBJECT_NAME(resource_associated_entity_id) tableName
FROM    sys.dm_tran_locks
WHERE   resource_type = 'OBJECT'
ORDER BY request_session_id ASC;

--根据锁表进程查询相应进程互锁的SQL语句
DBCC INPUTBUFFER (249);



--===========
--===========锁表进程
--解锁语句
DECLARE @spid INT;
SET @spid = 52;
DECLARE @SQL VARCHAR(1000);
SET @SQL = 'kill ' + CAST(@spid AS VARCHAR);
EXEC (@SQL);

--===========
--===========生成解锁SQL语句

SELECT
DISTINCT
        request_session_id ,
        'DECLARE @spid INT SET @spid = ' + request_session_id + ' '
        + ' DECLARE @SQL VARCHAR (1000) SET @SQL = ''kill '' + CAST (@spid AS VARCHAR) EXEC (@SQL);' AS 解锁语句
FROM    sys.dm_tran_locks
WHERE   resource_type = 'OBJECT'; --spid 锁表进程

 

posted @ 2020-07-07 13:39  山顶洞外人  阅读(126)  评论(0编辑  收藏  举报