Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to see a listing of all the stored procs for each database on a server along with when the last time that store proc was run. I'm pretty good with SQL but I don't know about looking at stats like this that sql keeps so I'd appreciate a little help finding this info.

EDIT:

From the answers I'm getting it sounds like this is not possible the way I thought it would be. I was thinking that it could be done similarly to how you can see when a table was last accessed:

select  t.name, user_seeks, user_scans, user_lookups, user_updates, 
        last_user_seek, last_user_scan, last_user_lookup, last_user_update

from    sys.dm_db_index_usage_stats i JOIN
        sys.tables t ON (t.object_id = i.object_id)

where   database_id = db_id()

The above script was stolen from a comment on http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/.

share|improve this question
add comment

3 Answers

Here is a little script that will list all of the user stored procedures (ie- those that didn't come with SQL Server or aren't system stored procedures) in all of the databases on your server instance:

CREATE TABLE #ProcTable (
    DbName sysname,
    ProcName sysname
)

DECLARE @command1 NVARCHAR(1000)

SET @command1 = 'USE [?];INSERT #ProcTable 
SELECT ''[?]'',Name
FROM sys.all_objects
WHERE TYPE=''p''
AND is_ms_shipped=0'

EXEC sp_msforeachdb @command1

SELECT * FROM #ProcTable 
ORDER BY DbName,ProcName

DROP TABLE #ProcTable 

If you're not running a trace that will capture the exec event (and don't have a default trace running) then you'll have to have an application level auditing mechanism in place to capture the running of stored procedures. If you don't have either of those going then you're not going to be able to go back in time to see when the procs have run.

share|improve this answer
    
Thanks. I guess I just assumed that since you can see that kind of information on tables that you'd be able to get it for sp's too. –  Brandon Moore Jun 5 '12 at 21:24
    
Well, there's no native execution logging. Are you talking about triggers? –  squillman Jun 5 '12 at 22:25
    
No, just thought sql server might keep stats on this info. See my edit. –  Brandon Moore Jun 6 '12 at 0:56
add comment

There's no native logging which does this, so you'll need to either run a server side trace or setup some sort of auditing to capture this information.

share|improve this answer
add comment

You can get a limited answer to that question by looking at the sys.dm_exec_procedure_stats dynamic management view that was introduced in SQL Server 2008.

select
    DB_NAME(database_id) as "database_name" 
    , OBJECT_NAME(object_id, database_id) as "procedure_name"
    , last_execution_time
from 
    sys.dm_exec_procedure_stats

For any stored procedure plans that are in the procedure cache, this will tell you the last execution time. (There are many more interesting columns in the DMV as well.)

This information isn't necessarily complete: at best it will have information only since the last start of the database engine (not persisted across service restarts.) If a stored procedure hasn't been run since the server was restarted, it won't be present here. Clearing the plan cache will cause this DMV to be reset. As will operations that clear the plan cache as a side effect (such as changing MAXDOP, or memory settings) or otherwise invalidate cached plans.

But it gives you a ballpark, and if you're feeling really ambitious you could write a routine that polls the DMV periodically and stores the results in a table for longer-term analysis.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.