Last active
September 27, 2019 20:09
-
-
Save dvaun/80670ae2b4f2012c92550daa14b971ad to your computer and use it in GitHub Desktop.
Grab last 50 executed, cached queries w/parameters from SQL Server - orig: https://stackoverflow.com/questions/3579092/tsql-get-last-queries-ran
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT TOP 50 * FROM(SELECT | |
COALESCE(OBJECT_NAME(s2.objectid),'Ad-Hoc') AS ProcName, | |
execution_count, | |
s2.objectid, | |
(SELECT TOP 1 | |
SUBSTRING( | |
s2.TEXT,statement_start_offset / 2+1, ( | |
( | |
CASE | |
WHEN statement_end_offset = -1 | |
THEN (LEN(CONVERT(NVARCHAR(MAX),s2.TEXT)) * 2) | |
ELSE statement_end_offset | |
END | |
) - statement_start_offset | |
) / 2+1 | |
) | |
) AS sql_statement, | |
SUBSTRING(s3.query_plan,CHARINDEX('<ParameterList>',s3.query_plan), CHARINDEX('</ParameterList>',s3.query_plan) + LEN('</ParameterList>') - CHARINDEX('<ParameterList>',s3.query_plan) ) AS Parameters, | |
last_execution_time | |
FROM | |
sys.dm_exec_query_stats | |
AS s1 | |
CROSS APPLY | |
sys.dm_exec_sql_text(s1.sql_handle) | |
AS s2 | |
CROSS APPLY | |
sys.dm_exec_text_query_plan(s1.plan_handle, s1.statement_start_offset, s1.statement_end_offset) | |
AS s3 | |
) x | |
WHERE | |
sql_statement | |
NOT like 'SELECT TOP 50 * FROM(SELECT %' | |
ORDER BY last_execution_time DESC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment