如何处理SQL Agent作业错误?
我正在编写一个 SQL 代理作业,每天一次从表 1 和表 2 中删除行。 SQL Agent作业的步骤是:
- 从表1中删除一些特定记录
- 从表2中删除一些特定记录。
我的问题是:
- 如何记录SQL Agent过程中的错误? 我们可以使用任何日志/事件吗? (这样我们就可以看到 SQL 代理作业在哪几天失败,以及失败的原因。)
- 我希望 SQL 作业继续执行(删除)表 2 中的特定记录,即使由于任何原因从表 1 中删除失败,任何最好做法如何落实?
I am writing a SQL Agent Job to remove rows from Table 1 and Table 2 once a day. The step of the SQL Agent job is:
- Delete some specific records from Table 1
- Delete some specific records from Table 2.
My question is:
- How to record the error during SQL Agent? Any logs/events we could use? (so that we could see in what days the SQL Agent Job failed, and why it failed.)
- I want the SQL Job to continue to execute (delete) specific records from Table2, even if for any reason delete from Table1 fails, any best practices how to implement this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你指的是 Microsoft SQL Server?
如果是的话,这两件事都很容易做到。
只需查看 SQL 代理作业的属性即可。 在“通知”下,您可以告诉作业写入 Windows 日志或向您发送电子邮件(但在此之前,您必须为每个服务器配置一次电子邮件)。
您可以选择是否仅在失败时发生、仅在成功时发生或始终发生。
您可以在作业中定义不同的步骤。 对于每个步骤,您可以定义失败时会发生什么以及成功时会发生什么(例如“转到下一步”或“结束有错误的作业”)。
编辑:
首先,这里是第一个问题的教程 - 如何设置通知,以便在作业失败时收到电子邮件。
对于第二个问题,您需要打开SQL Server代理作业的属性。
我在此处找到了属性窗口的英文屏幕截图。
看图片 - 您必须选择左侧的“步骤”,然后您可以在右侧看到作业的不同步骤。 请注意右侧的“成功时”和“失败时”栏,这就是我的意思。 您可以在编辑步骤时编辑这些值。
I assume you mean Microsoft SQL Server?
If yes, both things are quite easy to do.
Just look at the properties of you SQL Agent job. Under "Notifications", you can tell the job to write into the windows log or send you an e-mail (but before, you have to configure e-mail once per server).
You can choose if this should happen only when it fails, only when it's successful or always.
You can define different steps in a job. For each step, you can define what happens when if fails and what happens when it's successful (like "go to next step" or "end job with error").
EDIT:
First of all, here is a tutorial for your first question - how to set up notification so that you get an e-mail when the job fails.
For the second question, you need to open the properties of the SQL Server Agent job.
I found an English screenshot of the properties window here.
Look at the picture - you have to select "Steps" on the left, and then you can see the different steps of the job on the right. Note the columns "On success" and "On Failure" on the right side, that's what I meant. You can edit those values when you edit the step.