我不确定发生了什么?即使作业在晚上 11:50 停止运行,我仍然收到一封电子邮件,这显然不到 7 小时,因为它从昨晚 7 点开始。如果作业运行时间超过 7 小时,应该给我发电子邮件,并且出于某种原因,昨天它在测试中有效,但今天它不是……有什么想法吗?
SELECT *
FROM msdb..sysjobactivity aj
JOIN msdb..sysjobs sj on sj.job_id = aj.job_id
WHERE DATEDIFF(HOUR,aj.start_execution_date,GetDate())> 7
AND aj.start_execution_date IS NOT NULL
AND sj.name = 'Nightly_Job'
and not exists ( -- make sure this is the most recent run
select 1
from msdb..sysjobactivity new
where new.job_id = aj.job_id
and new.start_execution_date > aj.start_execution_date)
if @@ROWCOUNT > 0
BEGIN
USE msdb
EXEC sp_send_dbmail
@profile_name = 'DB_Mail',
@recipients = 'xxx@yyy.com',
@subject = 'T-SQL Query Result',
@body = 'The Nightly_Job has been running 7