use [msdb]
set nocount on
begin tran
declare @SubPlanName varchar(200)
--计划名称从Sqlserver 代理->作业查看,
--例如:BakUserDB.子计划,那该值就是“子计划”,赋值给下面的变量
set @SubPlanName='MaintenancePlan_Remotebak.Subplan_1' --\'子计划\'
--获得外键约束用到的列
declare @subPlan_id varchar(100),@job_id varchar(100)
,@msx_job_id varchar(100),@Schedule_id varchar(100)
select @job_id=job_id,@msx_job_id=msx_job_id,
@subPlan_id=subPlan_id from sysmaintplan_SubPlans where SubPlan_name=@SubPlanName
--按照顺序删除记录
if (@SubPlan_id is not null and @SubPlan_id<>'\')
delete from sysmaintplan_log where SubPlan_id=@SubPlan_id
if (@SubPlanName<>'\' and @SubPlanName is not null)
delete from sysmaintplan_SubPlans where SubPlan_Name=@SubPlanName
if (@Schedule_id is not null and @Schedule_id <>'\')
delete from SysSchedules where Schedule_id=@Schedule_id
if (@job_id is not null and @job_id<>'\')
delete from sysjobs where job_id=@job_id
/**//*--------------------------------------------------------------------
在上面的句子执行完后,该维护计划的接点依然存在,此时再更新msdb中对应的视图
---------------------------------------------------------------------*/
delete
FROM msdb.dbo.sysmaintplan_plans
--执行上述代码结束后,刷新维护计划的节点,该节点成功删除。
commit tran
if @@error<>0 Print 'no\'
else Print 'yes\'
set nocount off