Solved

SQL Alerting

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.

809 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