Query over results of 'sp_lock' in SQL Server

192 Views Asked by At

Theres is a built-in stored procedure in SQL Server called sp_lock. It will return a table which contains information about current locks in database server. It will return several columns including dbid which is id of each database. I want to query only one specific database id, but I don't know how to execute a query over returned result of sp_lock.

I want to know how to query over results of sp_lock?

1

There are 1 best solutions below

4
On

See the "Important" note in the documentation you link, you should be querying sys.dm_tran_locks not using this deprecated proc (Which sp_helptext 'sp_lock' indicates dates back to 1996), eg this will return locks in the current database:

select *
from sys.dm_tran_locks
where resource_database_id = db_id()