Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Alerting

Posted on 2015-01-26
5
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

722 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