Learn the storage and data-retrieval fundamentals of SQL Server 2016. Enroll Now.
[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More
Experts Exchange Solution brought to you by
"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.
Shows current locks. Even if it works across all database, ObjectName
populates for current database only. Could be modified with dynamic SQL if needed
Be careful with Query text for LOCKS with GRANT status. This represents currently active
request for this specific session id which could be different than query which produced locks
It also could be NULL if there are no active requests for this session
,DB_NAME(TL1.resource_database_id) as [DB Name]
WHEN 'OBJECT' THEN OBJECT_NAME(TL1.resource_associated_entity_id, TL1.resource_database_id)
WHEN 'DATABASE' THEN 'DB'
WHEN TL1.resource_database_id = DB_ID()
select OBJECT_NAME(object_id, TL1.resource_database_id)
where hobt_id = TL1.resource_associated_entity_id
'(Run under DB context)'
END as ObjectName
,WT.wait_duration_ms as [Wait Duration (ms)]
(ER.statement_start_offset / 2) + 1,
END - ER.statement_start_offset) / 2) + 1)
cross apply sys.dm_exec_sql_text(ER.sql_handle) S
TL1.request_session_id = ER.session_id
) as [Query]
sys.dm_tran_locks as TL1 left outer join sys.dm_os_waiting_tasks WT on
TL1.lock_owner_address = WT.resource_address and TL1.request_status = 'WAIT'
TL1.request_session_id <> @@SPID
Open in new window
Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.
when 0 then 'Unspecified' when 1 then 'ReadUncomitted'
when 2 then 'ReadCommitted' when 3 then 'Repeatable'
when 4 then 'Serializable' when 5 then 'Snapshot'
end as transaction_isolation_level,
request_session_id, resource_type, resource_subtype, request_mode, request_type, request_status, request_owner_type,
case when resource_type = 'object' then object_name(resource_associated_entity_id)
when resource_type = 'database' then db_name(resource_associated_entity_id)
when resource_type in ('key','page') then (select object_name(object_id) from sys.partitions where hobt_id = resource_associated_entity_id)
else cast(resource_associated_entity_id as varchar(20))
from sys.dm_tran_locks dtl
left outer join sys.dm_exec_sessions des
on dtl.request_session_id = des.session_id
where request_session_id <> @@spid
Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.