[英]Alert when stored procedure fails
Is it possible to have SQL Server (2008)
send an email alert if a stored procedure
fails for any reason? 如果
stored procedure
由于任何原因失败,是否可以让SQL Server (2008)
发送电子邮件警报?
I can do it quite easily from a SQL job but can't see any options within the Programmability
area or the properties of the stored procedure
itself. 我可以通过SQL作业很容易地做到这一点,但是看不到
Programmability
区域内的任何选项或stored procedure
本身的属性。 This stored procedure
is triggered via another application on demand. 该
stored procedure
通过另一个按需应用stored procedure
触发。 I have looked at doing it from the other application but this adds many layers of complication-I was hoping SQL Server
had me covered on this!? 我已经看过从其他应用程序执行此操作,但这增加了许多复杂性-我希望
SQL Server
可以覆盖我!
I have searched but not found anyone with the same question. 我已经搜索过,但没有发现任何有相同问题的人。
Thanks. 谢谢。
High level overview of how you can do this.. 有关如何执行此操作的高级概述。
1.Modify stored Procs to Return 1 in case of failure and log into table.This also has some weakness because some failures wont be Caught 1,修改存储的Procs以防万一失败并返回表并登录到表中,这也存在一些弱点,因为某些错误不会被捕获
2.Once the information is logged into table,then sending email is easy using SP_SEND_DBMAIL . 2.将信息记录到表中之后,使用SP_SEND_DBMAIL即可轻松发送电子邮件。
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Adventure Works Administrator',
@recipients = 'yourfriend@Adventure-Works.com',
@query = 'SELECT COUNT(*) FROM AdventureWorks2012.Production.WorkOrder
WHERE DueDate > ''2004-04-30''
AND DATEDIFF(dd, ''2004-04-30'', DueDate) < 2' ,
@subject = 'stored procedures Failures'
If i want to send email,i will use above approach instead of sending email for every failure.Since i logged data into a table,i can run a SQL job every 1 hour or so depending on frequency i need 如果我要发送电子邮件,我将使用上述方法,而不是每次失败都发送电子邮件。由于我已将数据记录到表中,因此我可以每隔1小时左右运行一次SQL作业,具体取决于我需要的频率
是的,这非常简单,只需使用常规错误处理(例如TRY / CATCH),然后在catch块中使用sp_send_dbmail将电子邮件发送给您需要的任何收件人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.