September 24, 2009 at 9:23 am
Hi All,
I have an SQL Agent script that runs at Midnight GMT every day. The script will send an email with a information on failures of a given number of errors per line item. Some days it does not have any alarms.
How do I modify the script so that it will not send the email if the number of errors are below the threshold?
Here is the script;
---------------------------------------------
USE NMS_RT
DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'
' +
N'
Wayside Name | State | Alarm Message | Error Count | Error Date | Alarm State |
---|
' ;
EXEC msdb.dbo.sp_send_dbmail
@recipients='usergroup@fabricam.com',
@subject = 'Alarms for Previous 24 Hours',
@body = @tableHTML,
@body_format = 'HTML',
@profile_name = 'test';
-----------------------------------
Thanks,
Retired Navy Chief Petty Officer, Consulting Systems Engineer, C# Programmer, SQL Beginner
September 24, 2009 at 9:36 am
Just wrap the sql to send the email in a IF...BEGIN END
e.g:
if (Some SQL Statement that returns a count of errors) >= 1
begin
print 'send email'
end
else
print 'dont send email'
September 24, 2009 at 9:38 am
i know it's kind of repetitive, but htis is how i've done it...use an exists to test for the condition, then the query to build the email, same as you've done:
IF EXISTS(SELECT 1
FROM RT_MCP_Historic_Alarms
WHERE [RT_MCP_Date_Time] >= Convert(char(10), DateADD(DAY, -1, GETDATE()), 101)
AND [RT_MCP_Date_Time] 0
GROUP BY RT_MCP_Name, RT_MCP_State, RT_MCP_Alarm_Text, RT_MCP_Bit_State, CONVERT(char(10), RT_MCP_Date_Time, 111)
HAVING COUNT(RT_MCP_Alarm_Text) >= 30 )
BEGIN
--paste your commands here
END
Lowell
September 24, 2009 at 11:03 am
Lowell are you saying it should look like this then;
-------------------------------------------------
USE NMS_RT
IF EXISTS(SELECT 1
FROM RT_MCP_Historic_Alarms
WHERE [RT_MCP_Date_Time] >= Convert(char(10), DateADD(DAY, -1, GETDATE()), 101) AND [RT_MCP_Date_Time] 0
GROUP BY RT_MCP_Name, RT_MCP_State, RT_MCP_Alarm_Text, RT_MCP_Bit_State, CONVERT(char(10), RT_MCP_Date_Time, 111)
HAVING COUNT(RT_MCP_Alarm_Text) >= 30 )
BEGIN
DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'
' +
N'
Wayside Name | State | Alarm Message | Error Count | Error Date | Alarm State |
---|
' ;
EXEC msdb.dbo.sp_send_dbmail
@recipients='usergroup@fabricam.com',
@subject = 'MCP Alarms for Previous 24 Hours',
@body = @tableHTML,
@body_format = 'HTML',
@profile_name = 'test';
END
Retired Navy Chief Petty Officer, Consulting Systems Engineer, C# Programmer, SQL Beginner
September 24, 2009 at 11:13 am
brian yes, that is exactly how i would do it;
to test it, simply run the sql inside the EXISTS() portion....it probably returns nothing, so change the HAVING to1 instead of 30...then you could see that that would work....
Lowell
September 24, 2009 at 11:21 am
When I execute the query, it returns this;
Msg 207, Level 16, State 1, Line 14
Invalid column name 'RT_MCP_Name'.
Msg 207, Level 16, State 1, Line 15
Invalid column name 'RT_MCP_State'.
Msg 207, Level 16, State 1, Line 16
Invalid column name 'RT_MCP_Alarm_Text'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'RT_MCP_Alarm_Text'.
Msg 207, Level 16, State 1, Line 18
Invalid column name 'RT_MCP_Date_Time'.
Msg 207, Level 16, State 1, Line 19
Invalid column name 'RT_MCP_Bit_State'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'RT_MCP_Date_Time'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'RT_MCP_Alarm_Text'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'RT_MCP_Name'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'RT_MCP_State'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'RT_MCP_Alarm_Text'.
Msg 207, Level 16, State 1, Line 20
Invalid column name 'RT_MCP_Bit_State'.
I am guessing I need to change something else also.
Thanks,
Brian
Retired Navy Chief Petty Officer, Consulting Systems Engineer, C# Programmer, SQL Beginner
September 24, 2009 at 11:29 am
that looks more like an error from running the script on the wrong database.
if the original SQL you pasted was valid, the rest should be valid too...
what you pasted is not the same as the original...what you pasted does not have a FROM statement...so it would fail.
the forum seems to chop some stuff out due to HTML that you want as part of your email.
the attached file passes syntax, so if the table and column objects are there, you should be good to go.
Lowell
September 24, 2009 at 11:43 am
Yes, it was the correct database. I can take the original code, open a new query and past the original code into it and it completes fine.
Retired Navy Chief Petty Officer, Consulting Systems Engineer, C# Programmer, SQL Beginner
September 24, 2009 at 12:10 pm
Your right. I grabbed the wrong copy.
And here is the revised code. It completes however it still sends the email out;
USE NMS_RT
IF EXISTS(SELECT 1
FROM RT_MCP_Historic_Alarms
WHERE [RT_MCP_Date_Time] >= Convert(char(10), DateADD(DAY, -1, GETDATE()), 101) AND [RT_MCP_Date_Time] 0
GROUP BY RT_MCP_Name, RT_MCP_State, RT_MCP_Alarm_Text, RT_MCP_Bit_State, CONVERT(char(10), RT_MCP_Date_Time, 111)
HAVING COUNT(RT_MCP_Alarm_Text) >= 1 )
BEGIN
DECLARE @tableHTML NVARCHAR(MAX) ;
SET @tableHTML =
N'
' +
N'
Wayside Name | State | Alarm Message | Error Count | Error Date | Alarm State |
---|
' ;
EXEC msdb.dbo.sp_send_dbmail
@recipients='usergroup@fabricam.com',
@subject = 'MCP Alarms for Previous 24 Hours',
@body = @tableHTML,
@body_format = 'HTML',
@profile_name = 'test';
END
Retired Navy Chief Petty Officer, Consulting Systems Engineer, C# Programmer, SQL Beginner
September 24, 2009 at 12:22 pm
yes...because the test is HAVING COUNT(RT_MCP_Alarm_Text) >= 1, it probably sends for testing purposes...change it back to 30, and it won't until you finally have 30 or more messages and also call your routine.
Lowell
September 24, 2009 at 12:27 pm
Duh! It has been one of those weeks. Thank you very much. That made it work exactly as I wanted it.
Brian
Retired Navy Chief Petty Officer, Consulting Systems Engineer, C# Programmer, SQL Beginner
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply