如果作业中的任何步骤失败,通知操作员
Posted
技术标签:
【中文标题】如果作业中的任何步骤失败,通知操作员【英文标题】:Notify Operator if ANY step in job fails 【发布时间】:2011-04-20 23:36:55 【问题描述】:我能否(如何)配置 Sql Server 2008 以在作业中的任何步骤失败时通知操作员?
我有一个 Sql Server 作业,其中包含几个步骤来更新来自多个不同来源的数据,然后是最后一个步骤,该步骤对数据执行多个计算。所有“数据刷新”步骤都设置为“失败时转到下一步”。一般来说,如果其中一个数据刷新失败,我仍然希望运行最后一步,但我仍然希望收到有关中间失败的通知,所以如果它们持续失败,我可以调查。
【问题讨论】:
【参考方案1】:这是我们的做法。我们用这个添加最后一个 T-SQL 步骤(通常称为“检查步骤”)
SELECT step_name, message
FROM msdb.dbo.sysjobhistory
WHERE instance_id > COALESCE((SELECT MAX(instance_id) FROM msdb.dbo.sysjobhistory
WHERE job_id = $(ESCAPE_SQUOTE(JOBID)) AND step_id = 0), 0)
AND job_id = $(ESCAPE_SQUOTE(JOBID))
AND run_status <> 1 -- success
IF @@ROWCOUNT <> 0
RAISERROR('Ooops', 16, 1)
请注意,此代码使用tokens in job steps($(...)
部分),因此无法在 SSMS 中按原样执行代码。它基本上尝试在sysjobhistory
中查找当前作业的先前步骤的条目并查找失败状态。
在“属性”->“高级”中,您还可以选中 在历史记录中包含步骤输出以获取步骤失败的消息。将失败操作留到退出报告失败的作业。
【讨论】:
漂亮而巧妙。我已经将它扩展成一些不太漂亮但很有帮助的东西;看我的回答。【参考方案2】:@wqw 接受的答案非常好。
我已经为那些启用了数据库邮件的人扩展了它,以便通过电子邮件发送更多关于失败的确切原因和方式的详细信息。还包含 icvader 在此页面上的答案以考虑重试。
对于我们这些需要更多细节来判断在场外/待命时是否需要采取紧急行动的人来说,这应该会很有帮助。
DECLARE
@YourRecipients as varchar(1000) = 'myadminemail@bloatcorp.com'
,@YourMailProfileName as varchar(255) = 'Database Mail'
,@Msg as varchar(1000)
,@NumofFails as smallint
,@JobName as varchar(1000)
,@Subj as varchar(1000)
,@i as smallint = 1
---------------Fetch List of Step Errors------------
SELECT *
INTO #Errs
FROM
(
SELECT
rank() over (PARTITION BY step_id ORDER BY step_id) rn
, ROW_NUMBER() over (partition by step_id order by run_date desc, run_time desc) ReverseTryOrder
,j.name job_name
,run_status
, step_id
, step_name
, [message]
FROM msdb.dbo.sysjobhistory h
join msdb.dbo.sysjobs j on j.job_id = h.job_id
WHERE instance_id > COALESCE((SELECT MAX(instance_id) FROM msdb.dbo.sysjobhistory
WHERE job_id = $(ESCAPE_SQUOTE(JOBID)) AND step_id = 0), 0)
AND h.job_id = $(ESCAPE_SQUOTE(JOBID))
) as agg
WHERE ReverseTryOrder = 1 ---Pick the last retry attempt of each step
AND run_status <> 1 -- show only those that didn't succeed
SET @NumofFails = ISNULL(@@ROWCOUNT,0)---Stored here because we'll still need the rowcount after it's reset.
-------------------------If there are any failures assemble email and send ------------------------------------------------
IF @NumofFails <> 0
BEGIN
DECLARE @PluralS as char(1) = CASE WHEN @NumofFails > 1 THEN 's' ELSE '' END ---To make it look like a computer knows English
SELECT top 1 @Subj = 'Job: ' + job_name + ' had ' + CAST(@NumofFails as varchar(3)) + ' step' + @PluralS + ' that failed'
,@Msg = 'The trouble is... ' +CHAR(13) + CHAR(10)+CHAR(13) + CHAR(10)
FROM dbo.#Errs
WHILE @i <= @NumofFails
BEGIN
SELECT @Msg = @Msg + 'Step:' + CAST(step_id as varchar(3)) + ': ' + step_name +CHAR(13) + CHAR(10)
+ [message] +CHAR(13) + CHAR(10)+CHAR(13) + CHAR(10) FROM dbo.#Errs
WHERE rn = @i
SET @i = @i + 1
END
exec msdb.dbo.sp_send_dbmail
@recipients = @YourRecipients,
@subject = @Subj,
@profile_name = @YourMailProfileName,
@body = @Msg
END
与其所依据的其他答案的一个区别是:不会将整个工作视为错误。 这是为了在作业历史记录中保留 Aborted 和 Completed with Errors 之间的区别。
【讨论】:
【参考方案3】:我的大部分步骤都设置为重试,因为一个独特的 translog 场景会导致偶尔阻塞。 wqw的帖子即使重试成功也会提示。我做了一个调整,如果一个步骤失败了,它不会发出警报,但在重试时会成功。
SELECT step_id, MIN(run_status)
FROM msdb.dbo.sysjobhistory
WHERE instance_id > COALESCE((SELECT MAX(instance_id) FROM msdb.dbo.sysjobhistory
WHERE job_id = $(ESCAPE_SQUOTE(JOBID)) AND step_id = 0), 0)
AND job_id = $(ESCAPE_SQUOTE(JOBID))
GROUP BY step_id
HAVING MIN(run_status) <> 1 -- success
IF @@ROWCOUNT <> 0
RAISERROR('FailedStep', 16, 1)
【讨论】:
【参考方案4】:对上述答案的改进,以防有人想使用 sql server agent 中的运算符发送电子邮件;并使用存储在 msdb 中的数据库配置文件名称:
DECLARE @EmailRecipients as varchar(1000)
DECLARE @MailProfileName as varchar(255)
DECLARE @Msg as varchar(1000)
DECLARE @NumofFails as smallint
DECLARE @JobName as varchar(1000)
DECLARE @Subj as varchar(1000)
DECLARE @i as smallint = 1
SELECT @EmailRecipients = email_address
FROM msdb.dbo.sysoperators
WHERE name = <Operator Name>
SELECT TOP(1) @MailProfileName = name
FROM msdb.dbo.sysmail_profile
SELECT * INTO #Errs
FROM
(SELECT rank() over (PARTITION BY step_id ORDER BY step_id) rn,
ROW_NUMBER() over (partition by step_id order by run_date desc, run_time desc) ReverseTryOrder,
j.name job_name,
run_status,
step_id,
step_name,
[message]
FROM msdb.dbo.sysjobhistory h
JOIN msdb.dbo.sysjobs j ON j.job_id = h.job_id
WHERE instance_id > COALESCE((SELECT MAX(instance_id) FROM msdb.dbo.sysjobhistory
WHERE job_id = $(ESCAPE_SQUOTE(JOBID)) AND step_id = 0), 0)
AND h.job_id = $(ESCAPE_SQUOTE(JOBID))
) AS agg
WHERE ReverseTryOrder = 1 ---Pick the last retry attempt of each step
AND run_status <> 1 -- show only those that didn't succeed
SET @NumofFails = ISNULL(@@ROWCOUNT,0)---Stored here because we'll still need the rowcount after it's reset.
IF @NumofFails <> 0
BEGIN
DECLARE @PluralS as char(1) = CASE WHEN @NumofFails > 1 THEN 's' ELSE '' END
SELECT top 1 @Subj = job_name + ':'+ CAST(@NumofFails as varchar(3)) + '''Check Steps'' Report',
@Msg = '''Check Steps'' has reported that one or more Steps failed during execution of ' + job_name + CHAR(13) + CHAR(10)+ CHAR(13) + CHAR(10)
FROM dbo.#Errs
WHILE @i <= @NumofFails
BEGIN
SELECT @Msg = @Msg + 'Step ' + CAST(step_id as varchar(3)) + ': ' + step_name +CHAR(13) + CHAR(10)
+ [message] +CHAR(13) + CHAR(10)+CHAR(13) + CHAR(10)
FROM dbo.#Errs
WHERE rn = @i
SET @i = @i + 1
END
EXEC msdb.dbo.sp_send_dbmail
@recipients = @EmailRecipients,
@subject = @Subj,
@profile_name = @MailProfileName,
@body = @Msg
END
【讨论】:
我没有仔细研究过这个,但我会说一件事。它的样式比我当时编写 SQL 的方式更好。【参考方案5】:Adamantish 的回答是完美的解决方案(谢谢):完美无瑕.. 稍作修改。正如 wqw 之前所说,在 SSMS 中不起作用,将其添加为最后一步并运行作业。
WHERE instance_id > COALESCE
(
(
SELECT MAX(instance_id)
FROM msdb.dbo.sysjobhistory
WHERE job_id = '2XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX' AND step_id = 0), 0
)
AND h.job_id = '2XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXX'
)
as agg
【讨论】:
【参考方案6】:转到作业属性 > 通知选项卡 > 作业完成时要执行的操作
选中电子邮件复选框,然后从下拉列表中选择“当作业失败时”并保存作业。
阅读http://msdn.microsoft.com/en-us/library/ms191130.aspx的第四点
如果您想通过电子邮件通知操作员,请选中电子邮件,从列表中选择操作员,然后选择以下选项之一:
当作业成功时:当作业成功完成时通知操作员。
当作业失败时:在作业未成功完成时通知操作员。
作业完成时:通知操作员,无论完成状态如何。
【讨论】:
很遗憾,这并不能解决问题。首先,这将在完成后发送电子邮件,无论成功或失败。我希望只有在某个步骤失败时才能看到电子邮件。 -- 此外,完成电子邮件不包含任何指示单个步骤失败的文本。它只是说明“工作成功”。我怎么知道某个特定步骤失败了。 使用此选项“当作业失败时”在作业未成功完成时通知操作员。这意味着如果作业完成时有任何失败的步骤,您将收到通知(即使有任何失败的步骤,它也会完成作业步骤失败,因为错误转到下一步是为作业步骤指定的)。然后您可以查看工作历史以获取更多详细信息..怎么说! “当作业失败时”选项不会发送电子邮件,因为最后一步成功(又名“退出作业报告成功”)【参考方案7】:在每一步添加代码:
if @@error > 0
EXEC sp_send_dbmail @profile_name='DBATeam',
@recipients=dbadmin@somewhere.com',
@subject='SomeJob SomeStep failed',
@body='This is the body of SomeJob SomeStep failed'
【讨论】:
以上是关于如果作业中的任何步骤失败,通知操作员的主要内容,如果未能解决你的问题,请参考以下文章
错误:注意:无法通过电子邮件通知“操作员”。当作业失败时尝试发送电子邮件
SQL Server 2008 R2中配置作业失败后邮件发送通知