Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8310435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:27:34+00:00 2026-06-08T19:27:34+00:00

I am not sure what is going on? I still get an email even

  • 0

I am not sure what is going on? I still get an email even though the job stopped running at 11:50 pm which clearly is less than thean 7 hours being that it started at 7 pm last night..It is suppose to email me if the job runs longer than 7 hours and for some reason yesterday it worked in test but today its not..Any ideas???

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:27:36+00:00Added an answer on June 8, 2026 at 7:27 pm

    The problem with your query is that it only checks how long has passed since the job was started… it doesn’t take into account when the job completed.

    If there are no other executions of the job within 7 hours, then your check that limits the query to the most recent run doesn’t exclude the previously completed job… your query then returns the job that was started over 7 hours ago, but in this case completed within 4 hours.

    To fix your query to exclude completed jobs, modify your query as follows:

    SELECT *
    FROM 
        msdb..sysjobactivity aj
        JOIN msdb..sysjobs sj on sj.job_id = aj.job_id
    WHERE 
        DATEDIFF(HOUR, aj.start_execution_date, 
                ISNULL(aj.stop_execution_date, GetDate()) )> 7 
        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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not quite sure what's going on here but it seems as though Microsoft always
Not sure whats going on here, or what could be the integer in this
Not sure what's going on here, but my iPhone apps nav bar shows back
Not sure what's going on here... My controller methods looks like this: [HttpGet] public
I'm not sure what's going on, but on my own laptop, everything works okay.
I'm not sure what's going on here but here's a section I'm having problems
CSS is not my strong suit, and I am not sure what's going on
Not too sure what's going on here as this doesn't seem like standard practise
I'm not too sure what's going on here but I'm trying to echo a
I'm not quite sure what's going on, but several issues are occurring on our

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.