有什么方法可以在 SqlServerAgent 作业成功/失败时向不同的电子邮件发送通知?

Gen*_*нин 6 sql-server maintenance sql-server-2012

Windows Server 2008R2 上的 SQL Server 2012 R2

有什么方法可以将通知发送到不同的电子邮件:

  • Sql Server 代理作业成功发送到一个电子邮件地址,失败时发送到另一个电子邮件地址?
  • 关于特定工作步骤(但不是整个工作)的成功(或失败)?

在此处输入图片说明

Kin*_*hah 9

你必须使用 tsql 来做到这一点。

在主要工作步骤之后的工作步骤中,再添加 2 个工作步骤:

在此处输入图片说明

第 2 步 - 成功电子邮件

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'YOUR MAIL PROFILE',
    @recipients = 'COMPANYMAIL@COMPANY.COM',
    @body = 'Copy and Purge BAKs success !!',
    @subject = 'Copy and Purge BAKs success',
-- to send file attachments 
    @file_attachments = 'D:\logs\log.TXT',
  -- Change below if you want to include query results from a table in the email. 
  -- Note that below is not in HTML
    @query = 'select column1, column2 from table1';
Run Code Online (Sandbox Code Playgroud)

第 3 步 - 失败电子邮件

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'YOUR MAIL PROFILE',
    @recipients = 'COMPANYMAIL@COMPANY.COM',
    @body = 'Copy and Purge BAKs failed !!',
    @subject = 'Copy and Purge BAKs failed',
-- to send file attachments 
    @file_attachments = 'D:\logs\log.TXT',
  -- Change below if you want to include query results from a table in the email. 
  -- Note that below is not in HTML
    @query = 'select column1, column2 from table1';
Run Code Online (Sandbox Code Playgroud)

然后使用 tsql sp_send_dbmail

注意:如果您希望发送 HTML 电子邮件,我有一个脚本可以帮助您。