我可以(如何)配置 Sql Server 2008 以在作业中的任何步骤失败时通知操作员吗?
我有一个 Sql Server 作业,其中包含几个步骤来更新来自多个不同来源的数据,然后是最后一个步骤,该步骤对数据执行多个计算。所有“数据刷新”步骤都设置为“失败时转到下一步”。一般来说,如果其中一个数据刷新失败,我仍然希望最后一步运行,但我仍然希望收到有关中间失败的通知,所以如果它们持续失败,我可以调查。
我可以(如何)配置 Sql Server 2008 以在作业中的任何步骤失败时通知操作员吗?
我有一个 Sql Server 作业,其中包含几个步骤来更新来自多个不同来源的数据,然后是最后一个步骤,该步骤对数据执行多个计算。所有“数据刷新”步骤都设置为“失败时转到下一步”。一般来说,如果其中一个数据刷新失败,我仍然希望最后一步运行,但我仍然希望收到有关中间失败的通知,所以如果它们持续失败,我可以调查。
这是我们如何做到的。我们用这个添加最后一个 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)
请注意,此代码在作业步骤(部分)中使用令牌$(...)
,因此无法在 SSMS 中按原样执行代码。它基本上尝试查找当前作业的先前步骤的条目sysjobhistory
并查找失败状态。
在“属性”->“高级”中,您还可以选中“在历史记录中包含步骤输出”以获取步骤失败的消息。将On failure 操作保留为Quit the job reporting failure。
@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 之间的区别。
由于一个独特的 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)
对上述答案的改进,以防有人想使用 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
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
转到作业属性 > 通知选项卡 > 作业完成时要执行的操作
在该复选框下选中电子邮件复选框,然后从下拉列表中选择“当作业失败时”并保存作业。
阅读http://msdn.microsoft.com/en-us/library/ms191130.aspx上的第 4 点
如果您想通过电子邮件通知操作员,请选中电子邮件,从列表中选择操作员,然后选择以下选项之一:
作业成功时:作业成功完成时通知操作员。
当作业失败时:在作业未成功完成时通知操作员。
作业完成时:无论完成状态如何,都通知操作员。
在每个步骤中添加代码:
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'