SQL Server在电子邮件通知上的职位错误

发布于 2025-01-30 15:59:13 字数 188 浏览 5 评论 0原文

我已经在SQL托管实例上配置了数据库电子邮件,运营商等,以在作业失败时接收电子邮件。

在电子邮件中,我们得到了这样的东西“ yyy_job在步骤3上失败”。

但是我的问题是... 有没有办法在电子邮件正文上添加错误消息? 我一直在搜索此信息一个合适的答案。

先感谢您

I have configured a database email, operators, and such on my SQL managed instance, to receive an email when a job fails.

In the email, we get something like this "The yyy_job failed on step 3".

But my question is... Is there a way to add the error message on the body of the email? I've been searching for this, but can't fine a suitable answer.

Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尬尬 2025-02-06 15:59:13

据我所知,当作业失败时,没有办法在电子邮件通知中添加更多详细信息。

唯一的方法是实施您自己的通知过程。

https://www.sqlshack.com /在job-failure-in-in-in-sql-server/上报告和宣传

As far as I know there's no way to add further details to the email notifications when a job fails.

The only way is to implement your own notification process.

https://www.sqlshack.com/reporting-and-alerting-on-job-failure-in-sql-server/

很快妥协 2025-02-06 15:59:13

我们有类似的设置。我们有一个由几个步骤组成的SQL Server代理作业。

我以这样的方式进行了配置,使我们在作业启动时会收到通知电子邮件,并在完成另一封电子邮件完成后。最终电子邮件有两个版本 - 一个是成功的,另一个用于失败。

在工作结束时,有两个最后的步骤称为“电子邮件确定”和“电子邮件失败”。请注意,每个步骤如何具有“成功”和“失败”配置。

在我们的情况下,这就是“电子邮件确定”和“电子邮件失败”的步骤的样子:

https://i.sstatic.net/gg7l6.png“ alt =“ email ok”>

“

在我的情况下,我只是有不同的电子邮件主题,因此很容易过滤,因此电子邮件客户端。

您可以编写任何额外的T-SQL代码来针对msdb.dbo.sysjobhistory执行查询,并将相关结果包括在电子邮件中。

我不会在这里写一个完整的查询,但我想它看起来与下面的草图相似。如果您需要帮助,请提出另一个问题。

这就是您可以使用 msdb.dbo.sp_send_dbmail 在电子邮件文本中包括一些查询的结果:

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'ABC'
    ,@recipients = '[email protected]'
    ,@subject = 'Some subject line'
    ,@body = @VarBody
    ,@body_format = 'TEXT'
    ,@importance = 'NORMAL'
    ,@sensitivity = 'NORMAL'
    ,@query = N'
        -- show latest entry in the log for your job
        SELECT TOP(1)
            message, ...
        FROM
            msdb.dbo.sysjobhistory
        WHERE
            job_id = ''your job ID''
        ORDER BY
            instance_id DESC; 
        '
    ,@execute_query_database = 'msdb'
    ;

请查看文档以获取文档以获取文档的列表sp_send_dbmail的参数。上面的示例嵌入了查询结果。您也可以将其作为单独的文件附加。

We have a similar set up. We have a SQL Server Agent job that consists of several steps.

I configured it in such a way that we receive notification email when the job starts and another email when it finishes. There are two versions of the final email - one for success, another for failure.

job steps

At the end of the job there are two final steps called "Email OK" and "Email FAIL". Note how each of the steps have their "On Success" and "On Failure" configured.

This is how "Email OK" and "Email FAIL" steps look like in our case:

email OK

email FAIL

In my case I simply have different subjects of the emails, so it is easy to filter in the email client.

You can write any extra T-SQL code to execute a query against msdb.dbo.sysjobhistory and include the relevant result into the email.

I will not write a complete query here, but I imagine it would look similar to my sketch below. If you need help with that, ask another question.

This is how you can use msdb.dbo.sp_send_dbmail to include the result of some query into the email text:

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'ABC'
    ,@recipients = '[email protected]'
    ,@subject = 'Some subject line'
    ,@body = @VarBody
    ,@body_format = 'TEXT'
    ,@importance = 'NORMAL'
    ,@sensitivity = 'NORMAL'
    ,@query = N'
        -- show latest entry in the log for your job
        SELECT TOP(1)
            message, ...
        FROM
            msdb.dbo.sysjobhistory
        WHERE
            job_id = ''your job ID''
        ORDER BY
            instance_id DESC; 
        '
    ,@execute_query_database = 'msdb'
    ;

Have a look at the documentation for a list of parameters for sp_send_dbmail. Example above inlines the query result. You can also attach it as a separate file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文