Question

I have need to be able to generate two types of alerts:

  1. We have a sql job which runs every 1 hour. I need an alert to let us know the time it took to complete the job

  2. As the count for Table1 increases where Processed IS NULL, I need an alert to notify me of that event. The select statement is below:

    SELECT 
     COUNT(*)
    FROM [dbo].[Table1] 
    WHERE Processed IS NULL
    

    As the count of above table increases I need an alert.

How can I achieve this please? Can someone help me with some sql for the above two please. Thanks.

Was it helpful?

Solution

I find I receive more help and the help is more detailed when I'm very specific in my questions. I liked the detail of the tagging and the questions, it lets me know roughly what you would want and for what flavor of SQL. I would recommend to place the version of SQL and what you have tried or looked for and what did not make sense in the future, this way we can help you work towards a complete answer that will also further your understanding.

We also prefer if you break your questions up into more posts rather than one long honey do list. Especially if they are not related at all.

We have a sql job which runs every 1 hour. I need an alert to let us know the time it took to complete the job

There are history tables you can use for this. I would create another job step at the end. Here's an article on MSSQLTips by Chad Churchwell that talks about the history tables. I've appended a query for you from the site, you will need to replace the job's name.

select top 1
 j.name as 'JobName',
 run_date,
 run_time,
 msdb.dbo.agent_datetime(run_date, run_time) as 'RunDateTime',
 run_duration
From msdb.dbo.sysjobs j 
INNER JOIN msdb.dbo.sysjobhistory h 
 ON j.job_id = h.job_id 
where j.enabled = 1  --Only Enabled Jobs
and j.name = 'PUT YOUR JOB NAME HERE'
order by JobName, RunDateTime desc

If I were to improve this in the future, I'd use functions and do some more looking up to make the code dynamic regardless of the job. In this code you will have to adjust the name of the job but you should be able to strip out the current job name and replace the where clause with it if you do some digging.

Now to the email portion.

Brent Ozar's site has an article by Jes Borland regarding the email feature and setting it up. Here's a sample code that takes what I gave you above.

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Main DB Mail profile',
@recipients = 'WHO YOU WANT TO GET THE EMAIL',
@subject = 'Job last run duration',
@query = N'select top 1
 j.name as [JobName],
 run_date,
 run_time,
 msdb.dbo.agent_datetime(run_date, run_time) as [RunDateTime],
 run_duration
From msdb.dbo.sysjobs j 
INNER JOIN msdb.dbo.sysjobhistory h 
 ON j.job_id = h.job_id 
where j.enabled = 1  --Only Enabled Jobs
and j.name = ''PUT YOUR JOB NAME HERE''
order by JobName, RunDateTime desc;',
@attach_query_result_as_file = 1,
@query_attachment_filename = 'Outputfile.txt'

You'll need to replace a few things in there and you'll need to ensure your email setup is correct in SQL Server.

2nd question) select count(*)FROM [dbo].[Table1] where Processed is null As the count of above table increases I need an alert.

There are a few ways to handle this and honestly you're going to need to supply a little more information. The way I see it and the way you phrased it, I would put an AFTER trigger on the table for when a record is UPDATED or INSERTED into the table where Processed IS NULL. This can have a severe performance impact on your server and this needs to be thought through.

How soon does the alert need to trigger when the number increases? Instantly? In 5 minutes? In 10 minutes? In a hour? Sometime next week?

I would use a table that the procedure would log to and it would log the timestamp and the count at that moment. The second statement in that procedure would check to see if the number has increased since the last interval. If yes, it would send an email, if no, it would do nothing. This would be a scheduled job step.

Give this some thought and if my answer did not help; please let me know and after you have edited in some constructive feedback about what did / did not help, things you did not understand, or what resources you used that couldn't quite get you to where you needed, I can edit my answer and assist further.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top