Solved

SQL Alerting

Posted on 2015-01-26
5
175 Views
Last Modified: 2015-01-26
Hi,

I've need to monitor a stored procedure and if it returns no rows then issue an email alert.  I have the SQL statement which would be :-

exec [BDirectSFH].[Core].[Util_OrderDelayMonitor] 'E'

but I need a script that will do the above check and if no rows are returned then send an email alert out.  Sadly I have no scripting skills to be able to do this so wondered if anyone has done anything similar before and could share any sample code?

Thanks
James
0
Comment
Question by:Jamie786
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 40570401
Hi Jamie,
You could try something like the following:
exec [BDirectSFH].[Core].[Util_OrderDelayMonitor] 'E'

IF (@@ROWCOUNT < 1)
    BEGIN
        EXEC msdb.dbo.sp_send_dbmail
    	@profile_name = 'Profile Set Up in SQL Server for Database Mail',
    	@recipients = 'youremail@yourcompany.com',
    	@body = 'The stored procedure returned no rows',
    	@subject = 'Util_OrderDelayMonitor Row Count Check' ;
    END

Open in new window


If possibly it may be worth looking into possibility of putting this logic in the actual stored procedure itself.
The above uses Database mail to send the mail so Database Mail would have to be configured in your SQL server

Other option is a trigger on the table but would possibly need more info on exactly what you are doing to make a judgement call on this
0
 

Author Comment

by:Jamie786
ID: 40570568
Well there is an issue within a certain database which the software developer is looking into but when you run :-

exec [BDirectSFH].[Core].[Util_OrderDelayMonitor] 'E'

It returns several results, but if it returns no results then that instance is stuck and need to be corrected, so all I need is an email alert warning me that no rows have been returned.  So I'll give your code a go :)  Many thanks for that though!
0
 

Author Comment

by:Jamie786
ID: 40570580
another way that would be very handy would instead of the script trying to send an email could it create an event with it's own EVENT ID and name that way I could have the existing monitoring software lookout for this and alert us to that
0
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 500 total points
ID: 40570675
Hi Jamie,
You could try using the RAISERROR statement - so something like the following:

RAISERROR("No rows returned", 11, 1) WITH LOG
0
 

Author Closing Comment

by:Jamie786
ID: 40570891
Perfect exactly what I was looking for!... Many thanks!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
I have a large data set and a SSIS package. How can I load this file in multi threading?
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question