Tuesday, May 3, 2011

How to get job id of a maintenance plan

Should be a simple one but just posting here for someone looking for a quick reference.

SQL Server creates jobs for each sub plan in a maintenance plan.

1. Query to get list of maintenance plans, owner, creation date
select * from msdb.dbo.sysmaintplan_plans

2. Query to get the list of subplans inside a maitenance plan
SELECT * FROM msdb.dbo.sysmaintplan_subplans

3. Query to get the job ID for a maintenance plan
SELECT b.name [Mant Plan],a.name [Job Name],a.job_id FROM msdb..sysjobs a
INNER JOIN msdb.dbo.sysmaintplan_subplans c ON c.job_id = a.job_id
INNER JOIN msdb.dbo.sysmaintplan_plans b ON b.id = c.plan_id