SQL Server Maintenance plan history check status

1.6k views Asked by At

I need to monitor the maintenance plan if still running, I've found an old post ( thanks to Don SQL Server Maintenance plan history check for success or failure ) to query the log and get the jobs result, but this give only the result after finish:

SELECT
    mp.name AS [MTX Plan Name],
    msp.subplan_name AS [Sub Plan Name],    
    mpl.start_time AS [JobStart],
    mpl.end_time AS [JobEnd],
    mpl.succeeded AS [JobSucceeded]
FROM
    msdb.dbo.sysmaintplan_plans mp
    INNER JOIN msdb.dbo.sysmaintplan_subplans msp ON mp.id = msp.plan_id
    INNER JOIN msdb.dbo.sysmaintplan_log mpl ON msp.subplan_id = mpl.subplan_id
        AND mpl.task_detail_id = -- Get the most recent run for this database
            (SELECT TOP 1 ld.task_detail_id 
            FROM msdb.dbo.sysmaintplan_logdetail ld
            WHERE ld.command LIKE ('%['+db_name()+']%')
            ORDER BY ld.start_time DESC)

1

There are 1 answers

1
jana On

This query will give you active/running jobs which are related to Maintenance plans. Whatever is there means it is running now

SELECT sj.name
   , sja.*
FROM msdb.dbo.sysjobactivity AS sja
INNER JOIN msdb.dbo.sysjobs AS sj ON sja.job_id = sj.job_id
WHERE sja.start_execution_date IS NOT NULL
   AND sja.stop_execution_date IS NULL
   AND sja.job_id IN (SELECT job_id FROM msdb.dbo.sysmaintplan_subplans)

Let me know if you need help with integration to PRTG