Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Created May 6, 2024 14:59
Show Gist options
  • Save gitfvb/58260a52ab14ec5daff07bb6d30211b7 to your computer and use it in GitHub Desktop.
Save gitfvb/58260a52ab14ec5daff07bb6d30211b7 to your computer and use it in GitHub Desktop.
Useful SQLServer Queries

Show last queries and parameter

SELECT TOP 1000 * 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
,       last_execution_time, query_plan
FROM sys.dm_exec_query_stats AS s1
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 
cross apply sys.dm_exec_query_plan(plan_handle) as qp
) x
--WHERE sql_statement NOT like 'SELECT TOP 50 * FROM(SELECT %'
--and OBJECTPROPERTYEX(x.objectid,'IsProcedure') = 1
ORDER BY last_execution_time DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment