sql - How to be alerted via e-mail when unexpected data appear in database? -
i have database table collects sensor values (think temperatures). happens sensor values out of expected range (like below 0 when measure heating in furnace) means sensor or electronics faulty or in bad state. know situation, not implement via trigger. more externally. decision on how fix situation cannot done/implement application. e-mail perfect purpose, point of view. so, tried:
create procedure dbo.sp_check_and_mail_problems begin declare @q nvarchar(max) = n'' + n'select utc, sensor_no, sensor_value' + n'from dbo.sensor_values ' + n'where sensor_value < 0 , utc > ''2014-09-09'' ' + n'order utc asc ' declare @message nvarchar(max) = 'negative sensor values found!'; exec msdb.dbo.sp_send_dbmail @profile_name='xxx', @recipients='me@something.cz', @subject='alert: negative sensor values', @body=@message, @query=@q end however, query executed procedure, , e-mail sent. usual case 1 when sensors work, , alert should not sent. way need test first whether there unexpected record revealed select, , if there report, e-mail should sent.
is there simple way conversion of select result simple text tabular form, form msdb.dbo.sp_send_dbmail returns?
if sensor failed, there rather long serie of negative values. how can test there @ least 1 such value in result? is, how should test situation happened?
check variable @@rowcount after select see if rows returned. send email.
of course means have execute second query information need in email if error rate low should not problem.
as formatting output more "email friendly" have @ this question, highest voted not selected answer seems need.
for failed sensors modify first select statement count number of negative results per sensor, returning rows having clause returns count greater n (with n being set indicate failed sensor)
Comments
Post a Comment