First of all, you need a little stored procedure to create interval schedules.
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
Usage example:
DECLARE @ScheduleId int; EXEC spCreateSchedule_Interval @scheduleName = 'UserA_Schedule', @intervalType = 'minutes', @interval = 27, @ScheduleId = @ScheduleId OUT;
This should create a schedule to run every 27 minutes.
You may also need proc to create a schedule for a specific time:
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
Usage example:
DECLARE @ScheduleId INT; EXEC spCreateSchedule_ExactTime @scheduleName = 'UserB_Schedule', @timeToRun = '14:58:00', @ScheduleId = @ScheduleId OUT;
This should create a schedule to run every day at 14:58.
The above two procedures can be easily combined into one. Separated here for clarity and ease of maintenance. They can also be further extended, you can parameterize @freq_type, @freq_interval, etc. All you need is documentation: https://msdn.microsoft.com/pl-pl/library/ms366342(v=sql.110).aspx
Another step is updating existing schedules:
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
And use:
EXEC spUpdateSchedule_Interval @scheduleName = 'UserB_Schedule', @intervalType = 'minutes', @interval = 25; GO
Now you can create spUpdateSchedule_ExactTime by analogy.
The last thing you need is a stored procedure for deleting schedules:
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
And its use:
USE msdb GO EXEC spDeleteSchedule 'UserA_Schedule';
Or you can easily write an alternative that will use sched_id instead of sched_name (sp_delete_schedule can get any of them).
ATTENTION: When updating and deleting procedures, you can use names or identifiers to define schedules. Although the names are more friendly to people and I used them to make the examples easier to follow, I strongly recommend that you use identifiers instead. Names should not be unique, therefore, if you manage to create two schedules with the same names, then both deleting and updating procs will fail if you do not use sched_id as a parameter.