How can we find which table is locked in the database? Please, suggest.
Asked
Active
Viewed 1.5e+01k times
2 Answers
87
You can use sp_lock
(and sp_lock2
), but in SQL Server 2005 onwards this is being deprecated in favour of querying sys.dm_tran_locks
:
select
object_name(p.object_id) as TableName,
resource_type, resource_description
from
sys.dm_tran_locks l
join sys.partitions p on l.resource_associated_entity_id = p.hobt_id

Mitch Wheat
- 295,962
- 43
- 465
- 541
8
When reading sp_lock information, use the OBJECT_NAME( ) function to get the name of a table from its ID number, for example:
SELECT object_name(16003073)
EDIT :
There is another proc provided by microsoft which reports objects without the ID translation : http://support.microsoft.com/kb/q255596/

Learning
- 8,029
- 3
- 35
- 46
-
1Link is broken, and searching Microsoft's site for sp_lock2 no longer returns anything. I guess they have finally pulled the plug on this approach. – Bampfer Apr 04 '18 at 17:19