get jobs in progress

Posted on 2014-09-02
Last Modified: 2014-09-03

I have to modify this query because jobs in progress are not in the result :
set nocount on; select distinct REPLACE(REPLACE(REPLACE(,'[',''),']',''),' ', '')  +'|'+  case h.run_status when 0 then 'Failed' when 1 then 'Successful' when 3 then 'Cancelled' when 4 then 'In Progress' end as JobStatus from msdb..sysjobhistory h, msdb..sysjobs j where j.job_id = h.job_id and h.run_date = (select max(hi.run_date) from msdb..sysjobhistory hi where h.job_id = hi.job_id and hi.run_date is not null) and instance_id = (select max(hi.instance_id) from msdb.dbo.sysjobhistory hi where h.job_id = hi.job_id) order by 1

Open in new window

How can I modify it?

Question by:bibi92
    LVL 34

    Expert Comment

    by:Brian Crowe
    start with this and narrow down the columns you want as status doesn't make much sense for a job in progress.

    SELECT *
    FROM msdb.dbo.sysjobs_view AS J
    INNER JOIN msdb.dbo.sysjobactivity AS A
    	ON J.job_id = A.job_id
    WHERE A.run_Requested_date IS NOT NULL
    	AND A.stop_execution_date IS NULL

    Open in new window


    Author Comment

    Thanks but I have to keep the query for zabbix monitoring and check job status.
    LVL 34

    Expert Comment

    by:Brian Crowe
    I see...give me a sec to play with it.
    LVL 34

    Accepted Solution

    try this...

    SELECT J.job_id,,
                WHEN JA.start_execution_date IS NOT NULL AND JA.stop_execution_date IS NULL THEN 'In Progress'
                WHEN JH.run_status = 0 THEN 'Failed'
                WHEN JH.run_status = 1 THEN 'Successful'
                WHEN JH.run_status = 3 THEN 'Cancelled'
                ELSE 'Not Run'
          END AS JobStatus
    FROM msdb.dbo.sysjobactivity AS JA
    LEFT OUTER JOIN msdb.dbo.sysjobhistory AS JH
          ON JA.job_history_id = JH.instance_id
    INNER JOIN msdb.dbo.sysjobs AS J
          ON J.job_id = JA.job_id
    WHERE ja.session_id = (SELECT TOP 1 session_id FROM msdb.dbo.syssessions ORDER BY agent_start_date DESC)

    Author Closing Comment

    Thanks, it works

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    The steps for moving the system databases to a new location are documented in the following technical article: However sometimes after the moving process is finished, though SQL i…
    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now