Solved

SQL Alerting

Posted on 2015-01-26
5
169 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now