如果作业中的任何步骤失败,则通知操作员
我是否可以(如何)配置 Sql Server 2008 以在作业中的任何步骤失败时通知操作员?
我有一项 Sql Server 作业,其中包含几个步骤来更新来自多个不同源的数据,最后一步是对数据执行多项计算。所有“数据刷新”步骤均设置为“失败时转到下一步”。一般来说,如果其中一项数据刷新失败,我仍然希望运行最后一步,但我仍然希望收到有关中间失败的通知,因此如果它们持续失败,我可以进行调查。
Can I (How do I) configure Sql Server 2008 to notify an operator if any step in the Job fails?
I have a Sql Server job with several steps to update data from multiple different sources, followed by one final step which performs several calculations on the data. All of the "data refresh" steps are set to "Go to next step on failure". Generally speaking, if one of the data refreshes fails, I still want the final step to run, but I still want to be notified about the intermediate failures, so if they fail consistantly, I can investigate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我们是这样做的。我们添加最后一个 T-SQL 步骤(通常称为“检查步骤”),并
注意此代码使用 作业步骤中的令牌(
$(...)
部分),因此代码无法按原样在SSMS中执行。它基本上尝试在 sysjobhistory 中查找当前作业的先前步骤的条目并查找失败状态。在“属性”->“高级”中,您还可以选中在历史记录中包含步骤输出以获取步骤失败的消息。将失败操作保留为退出报告失败的作业。
Here is how we do it. We add one last T-SQL step (usually called "check steps") with this
Notice that this code is using tokens in job steps (the
$(...)
part), so code can't be executed in SSMS as is. It basicly tries to find entries of previous steps of the current job insysjobhistory
and looks for failure statuses.In Properties->Advanced you can also check Include step output in history to get the message from step failure. Leave the On failure action to Quit the job reporting failure.
@wqw 接受的答案非常好。
我已将其扩展为那些启用了数据库邮件的用户,可以通过电子邮件发送有关失败原因和失败方式的更多详细信息。还在此页面上合并了 icvader 的答案,以考虑重试。
对于我们这些需要更多细节来判断在异地/待命时是否需要采取紧急行动的人来说应该非常有帮助。
与它所基于的其他答案的一个区别是:不会将整个工作视为错误。
这是为了保留作业历史记录中“已中止”和“已完成但有错误”之间的区别。
@wqw 's accepted answer is excellent.
I've extended it for those who have Database Mail enabled to email a bit more detail about exactly what failed and how. Also incorporates icvader's answer on this page to take account of retries.
Should be really helpful for those of us who need more detail to judge whether urgent action is required when offsite/on-call.
One difference from the other answers on which its based: Doesn't raise the whole job as an error.
That's to retain the distinction in job history between Aborted and Completed with Errors.
对上述答案的改进,以防有人想使用 sql server 代理中的运算符发送电子邮件;并使用存储在 msdb 中的数据库配置文件名称:
An improvement to the above answer, in case somebody wants to use the operators in sql server agent to send email; and use the database profile name stored in msdb:
由于独特的跨日志场景会导致偶尔阻塞,因此我将大部分步骤设置为重试。即使某个步骤已成功重试,wqw 的帖子也会发出警报。我做了一个调整,如果步骤失败,则不会发出警报,但重试时会成功。
I have most of my steps set to retry due to a unique translog scenario that causes occasional blocking. wqw's post will alert even if a step has successfully retried. I have made an adaptation that will not alert if a step has failed, but then was a success on retry.
Adamantish 的答案是完美的解决方案(谢谢):完美地工作..小修改。正如 wqw 之前所说,在 SSMS 中不起作用,将其添加为最后一步并运行作业。
Adamantish's answer is the perfect solution (Thank you) : worked flawlessly.. minor edits. As wqw stated before, will not work in SSMS, add this as a last step and run the job.
转到作业属性>通知选项卡>作业完成时要执行的操作
,选中电子邮件复选框并从下拉列表中选择“当作业失败时”并保存作业。
阅读 http://msdn.microsoft.com/en-us/library 上的第四点/ms191130.aspx
如果要通过电子邮件通知操作员,请选中“电子邮件”,从列表中选择操作员,然后选择以下选项之一:
作业成功时:通知作业成功完成时通知操作员。
作业失败时:作业未成功完成时通知操作员。
作业完成时:通知操作员,无论完成状态如何。
Go to Job Properties > Notification tab > action to perform when the job completes
under that check the Email checkbox and select "When the job fails" from the dropdown and save the job.
Read 4th point on http://msdn.microsoft.com/en-us/library/ms191130.aspx
If you want to notify an operator by e-mail, check E-mail, select an operator from the list, and then select one of the following:
When the job succeeds: to notify the operator when the job completes successfully.
When the job fails: to notify the operator when the job completes unsuccessfully.
When the job completes: to notify the operator regardless of completion status.
在每个步骤中添加代码:
in each step add code: