0% found this document useful (0 votes)
8 views

New-blk

The document contains a SQL query that retrieves information about blocking sessions in a database. It uses Common Table Expressions (CTEs) to gather details about blocking and blocked sessions, including wait times, resource types, and associated SQL statements. The final output includes a comprehensive view of the blocking situation, ordered by session IDs.

Uploaded by

www.greeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

New-blk

The document contains a SQL query that retrieves information about blocking sessions in a database. It uses Common Table Expressions (CTEs) to gather details about blocking and blocked sessions, including wait times, resource types, and associated SQL statements. The final output includes a comprehensive view of the blocking situation, ordered by session IDs.

Uploaded by

www.greeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

WITH BlockingInfo AS (

SELECT
r.blocking_session_id AS BlockingSessionID,
r.session_id AS BlockedSessionID,
r.wait_type,
r.wait_time / 1000 AS WaitTimeInSeconds,
r.wait_time / 60000 AS WaitTimeInMinutes,
r.wait_resource,
DB_NAME(r.database_id) AS DatabaseName,
st.TEXT AS BlockedSQLStatement,
r.status,
r.command,
r.cpu_time,
r.reads,
r.writes,
r.start_time
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
WHERE r.blocking_session_id <> 0
),
BlockingObjects AS (
SELECT
tl.request_session_id AS SessionID,
tl.resource_type AS ResourceType,
tl.resource_database_id AS DatabaseID,
tl.resource_associated_entity_id AS ObjectID,
CASE
WHEN o.name IS NOT NULL THEN o.name
ELSE 'Unknown Object'
END AS ObjectName,
s.name AS SchemaName
FROM sys.dm_tran_locks tl
LEFT JOIN sys.objects o ON tl.resource_associated_entity_id = o.object_id
LEFT JOIN sys.schemas s ON o.schema_id = s.schema_id
WHERE tl.request_status = 'WAIT'
AND tl.resource_type IN ('OBJECT', 'PAGE', 'KEY')
)
SELECT
b.BlockingSessionID,
b.BlockedSessionID,
b.wait_type,
b.WaitTimeInSeconds,
b.WaitTimeInMinutes,
b.wait_resource,
bo.ObjectName AS LockedObjectName, -- Table or index causing the block
bo.SchemaName AS LockedObjectSchema, -- Schema of the blocked object
b.DatabaseName,
b.BlockedSQLStatement,
b.status,
b.command,
b.cpu_time,
b.reads,
b.writes,
b.start_time
FROM BlockingInfo b
LEFT JOIN BlockingObjects bo ON b.BlockedSessionID = bo.SessionID
ORDER BY BlockingSessionID, BlockedSessionID;

You might also like