How to find Last Execution Date of Stored Procedure
I have seens this common question on few SQL Server Forum that how can i get the last execution date for Stored Procedur or when was the stored procedure exedcuted last.
Now How to achive this?
Every SQL Stament sent to SQL Server is PARSED,COMPILEd and EXECTED.SQL Server maintains QUERY PLAN for each and every statement and keeps it in the system tables.
Which we can query using DMV’s.Query Plans are flushed out if SQL Server Service gets started manulally or automatically(In cluster mode)/runs update stats command / reindexing / usinf DBCC CACHE Clean Commands.
To get last execution date of the stored procedure if SQL Server has the Query Plan for it.
SELECT
qs.sql_handle,qs.statement_start_offset,qs.statement_end_offset,
qs.creation_time,qs.last_execution_time,qp.dbid,qp.objectid,st.text
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
CROSS APPLY sys.dm_exec_text_query_plan(qs.plan_handle, DEFAULT, DEFAULT) AS qp
WHERE st.text like ‘%USP_CDS_GetUserDetails%’