SQL: Search Recently Run Cached SQL Statements

5/2/2016 2:44:01 PM

SQL

Search for queries that were run against a SQL database.

DECLARE @filter NVARCHAR(200)

SET @filter = 'SELECT * FROM'
--SET @filter = ''

SELECT		QueryStats.last_execution_time
			,QueryStats.total_elapsed_time
			,SQLText.text AS [Script]

FROM		sys.dm_exec_query_stats AS QueryStats
			CROSS APPLY sys.dm_exec_sql_text(QueryStats.sql_handle) AS SQLText

WHERE		SQLText.text like '%' + @filter + '%'
			
ORDER BY	QueryStats.last_execution_time DESC