步骤失败时 SQL 作业的通知
Notifications for SQL Job when a step fails
试图找出是否有一种方法可以在 sql 作业中仅 1 个(或多个)步骤失败时设置电子邮件通知。我有一个有 9 个步骤的工作,但是每个步骤都需要设置为即使失败也可以继续下一步。因此,即使步骤失败,因为我需要它进入下一步,它也不会设置为 "report failure"。由于 SQL 服务器代理没有 "Report failure and go to next step" 的选项我想知道是否有人有任何解决方法来获得通知
您可以添加一个额外的作业步骤来查询 msdb
表中失败的步骤。在此之后,sp_send_dbmail
存储过程可用于发送包含错误的电子邮件(如果有任何错误)。 SYSJOBHISTORY
的 run_date
列是一个 int 列,所以日期将按照 YYYYMMDD 的格式。 run_status
列下方的过滤器将应用于各个步骤,无论整体作业结果如何,0 表示状态失败。
DECLARE @JobExecutionDate INT
--current date
SET @JobExecutionDate = CAST(CONVERT(VARCHAR(8),GETDATE(),112) AS INT)
--check if there were any errors first
IF EXISTS (SELECT h.instance_id
FROM MSDB.DBO.SYSJOBHISTORY h
INNER JOIN MSDB.DBO.SYSJOBS j ON h.job_id = j.job_id
INNER JOIN MSDB.DBO.SYSJOBSTEPS s ON j.job_id = s.job_id AND h.step_id = s.step_id
WHERE h.run_status = 0 AND h.run_date = @JobExecutionDate AND j.name = 'YourJobName')
BEGIN
DECLARE @Title VARCHAR(50)
SET @Title = CONCAT(CONVERT(VARCHAR(12), CAST(GETDATE() AS DATE), 109), ' Job Error Email')
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Your Database Mail Profile',
@recipients = 'ExampleEmail@Domain.com',
@query = 'SELECT j.[name] AS JobName,
s.step_name AS StepName,
h.run_date AS RunDate,
h.run_time AS RunTime,
h.sql_severity As ErrorSeverity,
h.message AS ErrorMessage
FROM MSDB.DBO.SYSJOBHISTORY h
INNER JOIN MSDB.DBO.SYSJOBS j ON h.job_id = j.job_id
INNER JOIN MSDB.DBO.SYSJOBSTEPS s ON j.job_id = s.job_id AND h.step_id = s.step_id
WHERE h.run_status = 0 AND h.run_date = CAST(CONVERT(VARCHAR(8),GETDATE(),112) AS INT) AND j.name = ''YourJobName''
',
@query_result_no_padding = 1,
@subject = @Title ;
END
试图找出是否有一种方法可以在 sql 作业中仅 1 个(或多个)步骤失败时设置电子邮件通知。我有一个有 9 个步骤的工作,但是每个步骤都需要设置为即使失败也可以继续下一步。因此,即使步骤失败,因为我需要它进入下一步,它也不会设置为 "report failure"。由于 SQL 服务器代理没有 "Report failure and go to next step" 的选项我想知道是否有人有任何解决方法来获得通知
您可以添加一个额外的作业步骤来查询 msdb
表中失败的步骤。在此之后,sp_send_dbmail
存储过程可用于发送包含错误的电子邮件(如果有任何错误)。 SYSJOBHISTORY
的 run_date
列是一个 int 列,所以日期将按照 YYYYMMDD 的格式。 run_status
列下方的过滤器将应用于各个步骤,无论整体作业结果如何,0 表示状态失败。
DECLARE @JobExecutionDate INT
--current date
SET @JobExecutionDate = CAST(CONVERT(VARCHAR(8),GETDATE(),112) AS INT)
--check if there were any errors first
IF EXISTS (SELECT h.instance_id
FROM MSDB.DBO.SYSJOBHISTORY h
INNER JOIN MSDB.DBO.SYSJOBS j ON h.job_id = j.job_id
INNER JOIN MSDB.DBO.SYSJOBSTEPS s ON j.job_id = s.job_id AND h.step_id = s.step_id
WHERE h.run_status = 0 AND h.run_date = @JobExecutionDate AND j.name = 'YourJobName')
BEGIN
DECLARE @Title VARCHAR(50)
SET @Title = CONCAT(CONVERT(VARCHAR(12), CAST(GETDATE() AS DATE), 109), ' Job Error Email')
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Your Database Mail Profile',
@recipients = 'ExampleEmail@Domain.com',
@query = 'SELECT j.[name] AS JobName,
s.step_name AS StepName,
h.run_date AS RunDate,
h.run_time AS RunTime,
h.sql_severity As ErrorSeverity,
h.message AS ErrorMessage
FROM MSDB.DBO.SYSJOBHISTORY h
INNER JOIN MSDB.DBO.SYSJOBS j ON h.job_id = j.job_id
INNER JOIN MSDB.DBO.SYSJOBSTEPS s ON j.job_id = s.job_id AND h.step_id = s.step_id
WHERE h.run_status = 0 AND h.run_date = CAST(CONVERT(VARCHAR(8),GETDATE(),112) AS INT) AND j.name = ''YourJobName''
',
@query_result_no_padding = 1,
@subject = @Title ;
END