views:

79

answers:

3

Is it possible to find out when a Stored Procedure was last accessed?

I tried the following:

SELECT * 
FROM sys.dm_db_index_usage_stats 
WHERE [database_id] = DB_ID()  
    AND [object_id] = OBJECT_ID('stored procedure name')

and it returns a blank resultset.

+2  A: 

Add a log entry as the first line of the stored procedures:

insert into dbo.ProcLog (procname, date) values ('MyProc',getdate())
Andomar
@Andomar, I'd use `OBJECT_NAME(@@PROCID)` instead of 'MyProc', much easier to maintain.
KM
+3  A: 

I believe this is possible should the sproc still be in the procedure cache on the server at which point you can query sys.dm_exec_query_stats.

After that you are down to logging and tracing I'm afraid.

krystan honour
Thanks for the information. It is a shame that you can't actually view when a SP was last executed. :o(
Ardman
+1  A: 

here is a generic line of code you can place in every procedure, it will include the proper procedure name, without hard coding it.

INSERT INTO YourLog 
        (RunDate,ProcedureName,...) 
    VALUES 
        (GETDATE(),OBJECT_NAME(@@PROCID),...)
KM

related questions