首先需要一个用于创建间隔计划的存储过程。
USE msdb
GO
CREATE PROCEDURE spCreateSchedule_Interval
@scheduleName NVARCHAR(255),
@intervalType VARCHAR(255), -- one of 'seconds', 'minutes', 'hours'
@interval int,
@ScheduleId int OUT
AS
BEGIN
-- determine time interval
DECLARE @intervalTypeInt INT;
IF @intervalType = 'seconds'
SET @intervalTypeInt = 2;
ELSE IF @intervalType = 'minutes'
SET @intervalTypeInt = 4;
ELSE IF @intervalType = 'hours'
SET @intervalTypeInt = 8;
EXEC msdb.dbo.sp_add_jobschedule
@job_name='NameOfTheJobToBeApplied', -- or you can use @job_id instead
@name=@scheduleName, -- you can later find the schedule to update/delete using this name, or the @ScheduleId
@enabled=1,
@freq_type=4, -- daily
@freq_interval=1, -- every day
@freq_subday_type=@intervalTypeInt, -- eg. 2 = seconds
@freq_subday_interval=@interval, -- eg. 15 - run every 15 seconds
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20160101, -- some date in the past to activate immediately, or put some date in the future for delay
@active_end_date=99991231, -- never end, or specify some valid date
@active_start_time=000000, -- active from 00:00:00 - caution: when creating multiple schedules use different time here, eg 000001, 000002, so that they not get started simultanously, as it might couse some errrors
@active_end_time=235959, -- active to 23:59:59
@schedule_id=@ScheduleID -- this will output the newly generated id, which can be used later to localize the schedule for update/delete
END;
GO
示例用法:
DECLARE @ScheduleId int;
EXEC spCreateSchedule_Interval
@scheduleName = 'UserA_Schedule',
@intervalType = 'minutes',
@interval = 27,
@ScheduleId = @ScheduleId OUT;
这应该会创建每 27 分钟运行一次的计划。
您还需要一个 proc 来创建特定时间的计划:
CREATE PROCEDURE spCreateSchedule_ExactTime
@scheduleName NVARCHAR(255),
@timeToRun TIME,
@ScheduleId int OUT
AS
BEGIN
DECLARE @StartTime INT;
SET @StartTime = DATEPART(hour, @timeToRun) * 10000 + DATEPART(minute, @timeToRun) * 100 + DATEPART(second, @timeToRun);
EXEC msdb.dbo.sp_add_jobschedule
@job_name='NameOfTheJobToBeApplied', -- or you can use @job_id instead
@name=@scheduleName, -- you can later find the schedule to update/delete using this name, or the @ScheduleId
@enabled=1,
@freq_type=4, -- daily
@freq_interval=1, -- every day
@freq_subday_type=1, -- At the specified time
@freq_subday_interval=1, -- once a day, probably not used
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20160101, -- some date in the past to activate immediately, or put some date in the future for delay
@active_end_date=99991231, -- never end, or specify some valid date
@active_start_time=@StartTime, -- active from 00:00:00 - caution: when creating multiple schedules use different time here, eg 000001, 000002, so that they not get started simultanously, as it might couse some errrors
@active_end_time=235959, -- active to 23:59:59
@schedule_id=@ScheduleID -- this will output the newly generated id, which can be used later to localize the schedule for update/delete
END;
GO
示例用法:
DECLARE @ScheduleId INT;
EXEC spCreateSchedule_ExactTime
@scheduleName = 'UserB_Schedule',
@timeToRun = '14:58:00',
@ScheduleId = @ScheduleId OUT;
这应该创建在每天 14:58 运行的计划。
上述两个过程可能很容易合并为一个。为了清晰和便于维护,在此处分开。
它们还可以进一步增强,您可以参数化 @freq_type、@freq_interval 等。
您需要的只是文档:https://msdn.microsoft.com/pl-pl/library/ms366342(v=sql.110).aspx
另一个步骤是更新现有计划的过程:
CREATE PROCEDURE spUpdateSchedule_Interval
@scheduleName NVARCHAR(255),
@intervalType VARCHAR(255), -- one of 'seconds', 'minutes', 'hours'
@interval int
--, @ScheduleId int -- you can use this instead of the firs param
AS
BEGIN
-- determine time interval
DECLARE @intervalTypeInt INT;
IF @intervalType = 'seconds'
SET @intervalTypeInt = 2;
ELSE IF @intervalType = 'minutes'
SET @intervalTypeInt = 4;
ELSE IF @intervalType = 'hours'
SET @intervalTypeInt = 8;
EXEC msdb.dbo.sp_update_schedule
--@schedule_id=@ScheduleID, -- you can use this instead of the line below, if you change the proc parameter
@name=@scheduleName,
--@new_name = @newName -- if you want to change the schedule name
@enabled=1,
@freq_type=4, -- daily
@freq_interval=1, -- every day
@freq_subday_type=@intervalTypeInt, -- eg. 2 = seconds
@freq_subday_interval=@interval, -- eg. 15 - run every 15 seconds
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20160101, -- some date in the past to activate immediately, or put some date in the future for delay
@active_end_date=99991231, -- never end, or specify some valid date
@active_start_time=000000, -- active from 00:00:00 - caution: when creating multiple schedules use different time here, eg 000001, 000002, so that they not get started simultanously, as it might couse some errrors
@active_end_time=235959 -- active to 23:59:59
END;
GO
及用法:
EXEC spUpdateSchedule_Interval
@scheduleName = 'UserB_Schedule',
@intervalType = 'minutes',
@interval = 25;
GO
您现在应该可以通过类比创建 spUpdateSchedule_ExactTime。
您需要的最后一件事 - 用于删除计划的存储过程:
USE msdb
GO
CREATE PROCEDURE spDeleteSchedule
@scheduleName VARCHAR(255)
AS
BEGIN
EXEC msdb.dbo.sp_delete_schedule @schedule_name = @scheduleName, @force_delete = 1;
END;
GO
及其用法:
USE msdb
GO
EXEC spDeleteSchedule 'UserA_Schedule';
或者您可以轻松编写替代方案,使用 schedule_id 而不是 schedule_name(sp_delete_schedule 可以得到其中任何一个)。
注意:
在更新和删除过程中,您可以使用名称或 ID 来标识计划。
虽然名称更人性化,并且我使用它们是为了更容易理解示例,但我强烈建议您使用 ID。
名称不强制唯一,所以如果你碰巧创建了两个同名的调度,那么删除和更新过程都会失败,除非你使用 schedule_id 作为参数。