与维护计划有关的表:
1.sysmaintplan_log(记录维护计划运行log); 2.sysjobschedules(记录代理作业的作业信息); 3.sysmaintplan_subplans(记录维护计划的子计划信息); 4.sysjobs_view(相关的作业信息)以上这4张表有着PK和FK的关联关系,在删除作业时,很容易因为发生FK的冲突而导致失败。
具体的处理方法如下:
use [msdb]
declare @job_name varchar(100)
set @job_name = N'job_name'
--注:job_name为维护计划对应的job name
删除在计划里面的日志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)
删除作业
delete from msdb.dbo.sysjobs_view where name = @job_name
特别注意必须首先删除sysmaintplan_log,若表里只有id一致的记录则直接清空sysmaintplan_log,sysmaintplan_subplans表即可。