views:

50

answers:

3

Hi All,

Hope you can help.

Is there a way to reliably detect when a stored proc is being run on SQL Server without altering the SP itself?

Here's the requirement. We need to track users running reports from our enterprise data warehouse as the core product we use doesn't allow for this. Both core product reports and a slew of in-house ones we've added all return their data from individual stored procs.

We don't have a practical way of altering the parts of the product webpages where reports are called from. We also can't change the stored procs for the core product reports. (It would be trivial to add a logging line to the start/end of each of our inhouse ones).

What I'm trying to find therefore, is whether there's a way in SQL Server (2005 / 2008) to execute a logging stored proc whenever any other stored procedure runs, without altering those stored procedures themselves.

We have general control over the SQL Server instance itself as it's local, we just don't want to change the product stored procs themselves.

Any one have any ideas? Is there a kind of "stored proc executing trigger"? Is there an event model for SQL Server that we can hook custom .Net code into? (Just to discount it from the start, we want to try and make a change to SQL Server rather than get into capturing the report being run from the products webpages etc)

Thoughts appreciated
Thanks

+2  A: 

You can only use SQL Profiler reliably: there is no execute hook or "execution trigger" to use.

Saying that, you can indirectly but not 100% reliably monitor the plan cache. See this link. However, if you have OPTION RECOMPILE then it is removed from the cache after use. Also, stats are reset after server start and you need to run the dmv to see what's in the cache too.

It depends how thorough you need to be...

gbn
we deal with a huge number of reports for the business. we need to collect some sort of stats on how often the reports are used and by how many distinct users. its important that the performance of the report doesn't skew how often we record it being used. ie - i discounted polling current running commands etc as that's more likely to detect long running procs that short running ones.
Arj
use a trace then: it's the only way. See Paul Williams' answer for "how to"
gbn
+5  A: 

You could setup a background trace that is run automatically when SQL Server initializes. Then, in your trace, you could output the trace statement to a table.

For example, open up SQL Server Profiler. Create the trace you would want, i.e. include SP:Starting and the columns you want. Also choose to save the trace to a table.

Now, after setting up the script, choose File/Export/Script Trace Definition. This will create a SQL statement that generates the trace.

Next, create a stored procedure that creates this trace using SQL. Install the proc in the master database and tell SQL Server to run it automatically on startup:

exec sp_procoption N'sp_MyProc', N'startup', N'true'

Now every time SQL Server restarts, it will configure your trace automatically, and all SP invocations will be logged to a table.

-- EDIT --

Also note that there are some Dynamic Management Views (DMVs) useful for monitoring stored procedures. Two you might be interested in:

For example, sys.dm_exec_procedure_stats will tell you when the proc was last run, how many times it was run, the longest execution time, etc. Note that these views only affect stored procedures currently in the database cache. If the proc is unloaded, so will be the information.

Paul Williams
Good answer....
gbn
thanks. the environment is highly intensive in terms of transactions. any ideas what kind of overhead this would add?
Arj
The information does not get transmitted to a SQL Server Profiler client, so there is no extra network traffic required. Every SP start would be logged, so that means a single row INSERT into your log table. How many SP's get executed per day? That's how large the table will grow. Note that you can choose to log to a file instead, if you're concerned about clogging up the database.
Paul Williams
also - is there any way to tell how many distinct logins requested the stored proc?
Arj
If you include the login information in the trace, then yes. Once you have the log data in a table, you can query it however you like.
Paul Williams
A: 

You can use a tool such as SQL Log Rescue(free) to query your SQL Log files http://www.red-gate.com/products/SQL_Log_Rescue/index.htm It can tell you which stored procedures have been run & when. This is the easiest method, since it doesn't involve modifying any existing code and doesn't increase the overhead of the applications & server.

Ed B
thanks ill have a look. do you know though is it's possible to determine stored proc name and the login that executed it?
Arj
How does it tell you what stored procs run? And if this is a reporting app it's read only mostly with nolog writes to "rescue"...
gbn