首页 > 代码库 > 查询数据库锁

查询数据库锁

 查询数据库锁的根源:

SELECT a.spid,a.blocked,a.waitresource,a.dbid,a.hostname,a.program_name,c.text AS a_sql ,b.hostname AS b_hostName,b.program_name AS b_programName,d.text AS b_sqlFROM sys.sysprocesses aINNER JOIN sys.sysprocesses b ON b.spid=a.blockedCROSS APPLY sys.dm_exec_sql_text(a.sql_handle) cCROSS APPLY sys.dm_exec_sql_text(b.sql_handle) dWHERE a.blocked<>0

 

查数据库所有被锁的spid

SELECT * FROM sys.sysprocesses a WHERE a.blocked<>0

 

查数据库id和name映射

-- 根据name查idSELECT DB_ID(users)-- 根据id查nameSELECT * from sys.databases WHERE database_id=15

 

查询数据库锁