教你使用sqlserver:查询锁住sql语句以及如何解锁方法

查看被锁表语句

–查看被锁表:
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
–spid 锁表进程
–tableName 被锁表名

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

解锁语句

— 解锁:
DECLARE
@spid INT
SET @spid = 52–锁表进程
DECLARE
@SQL VARCHAR (1000)
SET @SQL = ‘kill ‘ + CAST (@spid AS VARCHAR) EXEC (@SQL)

生成解锁SQL语句

–生成解锁SQL
SELECT
DISTINCT ‘DECLARE @spid INT SET @spid = ‘,request_session_id,’ DECLARE @SQL VARCHAR (1000) SET @SQL = ”kill ” + CAST (@spid AS VARCHAR) EXEC (@SQL);’ as s
FROM
sys.dm_tran_locks
WHERE
resource_type = ‘OBJECT’ –spid 锁表进程
–tableName 被锁表名

总结

查看被锁表语句

–查看被锁表:
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
–spid 锁表进程
–tableName 被锁表名

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

解锁语句

— 解锁:
DECLARE
@spid INT
SET @spid = 52–锁表进程
DECLARE
@SQL VARCHAR (1000)
SET @SQL = ‘kill ‘ + CAST (@spid AS VARCHAR) EXEC (@SQL)

生成解锁SQL语句

–生成解锁SQL
SELECT
DISTINCT ‘DECLARE @spid INT SET @spid = ‘,request_session_id,’ DECLARE @SQL VARCHAR (1000) SET @SQL = ”kill ” + CAST (@spid AS VARCHAR) EXEC (@SQL);’ as s
FROM
sys.dm_tran_locks
WHERE
resource_type = ‘OBJECT’ –spid 锁表进程
–tableName 被锁表名

总结

以上就是本文关于教你使用sqlserver:查询锁住sql语句以及如何解锁方法的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅:ORACLE SQL语句优化技术要点解析等,如果有什么问题,可以随时留言,小编会及时回复大家,努力给广大编程爱好及工作者提供更优质的文章及更好的阅读体验。感谢朋友们对本站的支持!


数据运维技术 » 教你使用sqlserver:查询锁住sql语句以及如何解锁方法