2020-02-08

use [msdb]

declare @job_name varchar(100)
set @job_name = N'5S.Subplan_1'
--注:jobName为维护计划对应的jobName
--删除在计划里面的日志
delete sysmaintplan_log from sysmaintplan_subplans as subplans
inner join sysjobs_view as syjobs on subplans.job_id = syjobs.job_id
inner join sysmaintplan_log on subplans.subplan_id =sysmaintplan_log.subplan_id
where (syjobs.name = @job_name)
--删除代理的作业
delete sysjobschedules from sysjobs_view v
inner join sysjobschedules o on v.job_id=o.job_id where v.name=@job_name
--删除子计划
delete sysmaintplan_subplans from sysmaintplan_subplans as subplans
inner join sysjobs_view as syjobs on subplans.job_id = syjobs.job_id
where (syjobs.name = @job_name)
--删除作业