Solved

SQL Alerting

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

14 Experts available now in Live!

Get 1:1 Help Now