SQL Swiss Army Knife #11.1 - Locking, blocking and active transactions
Published Mar 23 2019 12:54 PM 1,034 Views
First published on MSDN on Sep 18, 2012
Latest update: 12/2/2016 (Check change log )

Download on GitHub

Hello all,
Following the series "SQL Swiss Army Knife", here is another version of this script that I’ve been using and tweaking for years now. This way I can get a quick overview of query execution on a given SQL Server instance, from SQL Server 2005 up to SQL Server 2016.

The script leverages on some known DMVs ( sys.dm_exec_requests , sys.dm_exec_sessions , sys.dm_os_tasks , sys.dm_exec_query_plan , sys.dm_db_session_space_usage , sys.dm_resource_governor_workload_groups , sys.dm_db_task_space_usage and sys.dm_tran_locks ) and DMFs ( sys.dm_exec_sql_text , sys.dm_exec_query_plan and sys.dm_exec_query_statistics_xml ) to report on all running requests, waiter and blocking and SP execution statistics.

The first section will report on all requests. In here there is info on running batches, the statement in the batch that is currently running and its status. Also, there is information about the cached query plan, current I/O, CPU and time stats, the current wait type, resource (if any) and other useful data.

The output will look something like below, and you can get further detail on statements and plans by clicking on the XML links:

The second section is the Waiter and Blocking Report, where we find the queries that are blocked, such as the spid, its wait type, resource and time. Still on the blocked query, the associated batch, blocking elapsed time and database name. On the blockers, there is information on the spid, if it is the head blocker in a given blocking chain and what is the blocker batch. Additional information on the involved hosts and programs is also available.

As before, you can get further detail by clicking on the XML links:

And last, starting with SQL Server 2012, you can get some details on Stored Procedures execution statistics from the sys.dm_exec_procedure_stats DMV, like below:

That’s it for now, hope you find it useful.

Until next time!

Change log:






Initial release.



Added page type information (PFS; GAM or SGAM) when wait type is PAGELATCH_ or PAGEIOLATCH_.



Fixed parse page issue.



Added memory grant info, statements to blocking and blocked sections of blocking report, and fixed head blocker info.



Fixed blocking section showing non-blocked sessions.



Added information to blocking section, and fixed conversion issues.



Handle illegal characters in XML conversion.



support for SQL Server 2016 SP1 and live query plan snapshot.



Fixed transport-level error issue with SQL Server 2016 SP1.

Disclaimer: I hope that the information on these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Ezequiel. Further, Ezequiel shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.
1 Comment
Version history
Last update:
‎Mar 23 2019 12:54 PM
Updated by: