http://blog.csdn.net/whbo/archive/2008/04/24/2325099.aspx
代码
use [msdb]
set nocount on
begin tran
declare @SubPlanName varchar(200)
--计划名称从Sqlserver 代理->作业查看,
--例如:BakUserDB.子计划,那该值就是“子计划”,赋值给下面的变量
set @SubPlanName='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
delete
FROM msdb.dbo.sysmaintplan_plans
--执行上述代码结束后,刷新维护计划的节点,该节点成功删除。
commit tran
if @@error<>0 Print '执行失败'
else Print '执行成功'
set nocount off
set nocount on
begin tran
declare @SubPlanName varchar(200)
--计划名称从Sqlserver 代理->作业查看,
--例如:BakUserDB.子计划,那该值就是“子计划”,赋值给下面的变量
set @SubPlanName='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
delete
FROM msdb.dbo.sysmaintplan_plans
--执行上述代码结束后,刷新维护计划的节点,该节点成功删除。
commit tran
if @@error<>0 Print '执行失败'
else Print '执行成功'
set nocount off