SQL Query
SELECT
r.blocking_session_id AS BlockingSessionID,
DB_NAME(r.database_id) AS DatabaseName,
--OBJECT_NAME(object_id, r.database_id) AS BlockedObjectName,
--OBJECT_SCHEMA_NAME(object_id, r.database_id) AS BlockedObjectSchema,
r.database_id AS BlockedObjectName,
r.database_id AS BlockedObjectSchema,
st.text AS BlockedSQLText,
r.blocking_session_id AS BlockedBySessionID,
r.command AS BlockingCommand,
CASE WHEN r.transaction_id IS NULL THEN 'Not In Transaction' ELSE 'In Transaction' END AS BlockingInTransaction,
es.login_name AS BlockedUser,
er.blocking_session_id AS BlockingSessionID,
es.host_name AS BlockingHostName,
es.program_name AS BlockingProgram,
er.start_time AS BlockingStartTime
FROM
sys.dm_exec_requests AS r
JOIN
sys.dm_exec_sessions AS es ON r.session_id = es.session_id
JOIN
sys.dm_exec_connections AS ec ON es.session_id = ec.session_id
JOIN
sys.dm_exec_requests AS er ON ec.most_recent_session_id = er.session_id
CROSS APPLY
sys.dm_exec_sql_text(r.sql_handle) AS st
WHERE
r.session_id != @@SPID
AND r.blocking_session_id != 0
ORDER BY
r.blocking_session_id;
Let’s discuss this table definition and how this table will help us to make this query.
sys.dm_exec_requestspan
sys.dm_exec_requests is not a table in the traditional sense; rather, it's a Dynamic Management View (DMV) provided by Microsoft SQL Server.
- It serves as a valuable tool for database administrators and developers to monitor and manage the currently executing requests or sessions on the SQL Server instance.
- This dynamic management view is particularly useful for monitoring and troubleshooting database performance, identifying long-running queries, detecting blocking issues, and understanding resource utilization on the server. It provides valuable insights into the current activity happening on the SQL Server instance, allowing database administrators and developers to optimize and tune database operations effectively.
Overall, sys.dm_exec_requests is a critical tool for real-time performance monitoring and troubleshooting in SQL Server environments. It allows database administrators and developers to gain visibility into the activity happening on the server and take proactive measures to optimize performance and maintain stability.
sys.dm_exec_sessions
sys.dm_exec_sessions is another Dynamic Management View (DMV)provided by Microsoft SQL Server.
- It contains information about all active sessions on the SQL Server instance, including user sessions, system sessions, and internal background tasks.
- The intention of sys.dm_exec_sessions is to provide a comprehensive view of the current sessions connected to the SQL Server instance, along with various attributes and properties associated with each session. Here's how this DMV can be helpful.
Overall, sys. dm_exec_sessions serves as a valuable tool for monitoring and managing the sessions connected to a SQL Server instance. By querying this DMV, administrators can gain insights into session activity, resource utilization, blocking scenarios, and transactional behavior, allowing them to optimize server performance and troubleshoot issues effectively.
sys.dm_exec_connections
sys.dm_exec_connections is a Dynamic Management View (DMV) provided by Microsoft SQL Server.
It contains information about the current connections established to the SQL Server instance, including details about client connections, protocols, and network addresses.

Join the conversation! Your thoughts help the community grow.