views:

939

answers:

4

I would like to set up a SQL Server 2008 Alert to notify me when any procedure executes for 1 second or longer (just an example).

Any ideas?

EDIT:

Okay, it seems this is not possible. But, just to nudge you in another direction, I know that there are statistics tables in the master database which hold compilation counts, number of calls and other various statistics. Could I maybe periodically query them and then report on it, somehow?

+1  A: 

no there are no notifications for this. you'll have to set up a trace and poll it every now and then.

Mladen Prajdic
Can you give me a bit more details?
muerte
+2  A: 

If found this on sqlservercentral (it probably for an older version of SQL Server, but still might be relevant to 2008)

Alert Procedure for Long-Running Job http://www.sqlservercentral.com/scripts/Lock+and+Connection+Management/30144/

Text below if you cant access it.

For jobs that run periodically and should take only a short time to run, a DBA may want to know when the job has been running for an excessive time. In this case, just checking to see IF the job is running won't do; the ability to make sure that it hasn't been running for a long period is needed. Matching the job id to a process id in sysprocesses requires some re-arranging of the job id to do the match. This script creates a stored procedure that will accept a job name, maximum run time allowed, and email address to notify. It will then use the job name to re-string the job number and check sysprocesses (based on the process id being part of the program name) to determine the amount of time the job has been runing, then alert if that time is over the "time allowed" parameter.

CREATE proc sp_check_job_running @job_name char(50), @minutes_allowed int, @person_to_notify varchar(50) AS

DECLARE @var1 char(1), @process_id char(8), @job_id_char char(8), @minutes_running int, @message_text varchar(255)

select @job_id_char = substring(CAST(job_id AS char(50)),1,8) from msdb..sysjobs where name = @job_name

select @process_id = substring(@job_id_char,7,2) + substring(@job_id_char,5,2) + substring(@job_id_char,3,2) + substring(@job_id_char,1,2)

select @minutes_running = DATEDIFF(minute,last_batch, getdate()) from master..sysprocesses where program_name LIKE ('%0x' + @process_id +'%')

if @minutes_running > @minutes_allowed BEGIN select @message_text = ('Job ' + UPPER(SUBSTRING(@job_name,1,LEN(@job_name))) + ' has been running for ' + SUBSTRING(CAST(@minutes_running AS char(5)),1,LEN(CAST(@minutes_running AS char(5)))) + ' minutes, which is over the allowed run time of ' + SUBSTRING(CAST(@minutes_allowed AS char(5)),1,LEN(CAST(@minutes_allowed AS char(5)))) + ' minutes.') EXEC master..xp_sendmail @recipients = @person_to_notify, @message = @message_text, @subject = 'Long-Running Job to Check' END

-- Typical job step syntax for job to do the checking

execute sp_check_job_running 'JobThatSHouldBeDoneIn5Minutes', 5, '[email protected]'

kevchadders
This will check job performance. I need to check for every stored procedure executed in the database (or entire server).
muerte
A: 

You can log slow running queries use SQL Profiler, but that won't help you with an alert, specifically. (I suppose you could log to a File or to a database table, and have something else "listen" and raise an alert).

In case you are not familiar with SQL Profiler here are the steps:

You should run SQL Profiler on something other than the server itself.

Start with the SQLProfilerTSQL_Duration template

Set the Filter : Duration : "Greater than" to 1000 (milliseonds)

In the filters you can restrict the Database, Login, or similar if necessary.

In the Events you can restrict to just SProcs (default also includes SQL statements)

Kristen
Yeah, that's manual work and not automated (no alerts).
muerte
Would something like ServersAlive do? it could be set up to run a query against a SQL Server table for something like "Any new log entry in the last N minutes". ServersAlive can alert by SMTP, SMS, Pager etc. www.woodstone.nu/salive I expect SQL Agent could do something similar.
Kristen
A: 

If you are not looking for a free solution, you could take a look at SQL Sentry. It does what you ask for and more.

Hth,
Lieven

Lieven