需要的第一件事是创建间隔计划的一些存储过程。
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
@[email protected], -- 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
@[email protected], -- eg. 2 = seconds
@[email protected], -- 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
@[email protected] -- 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分钟运行一次。
,您还可以根据需要一个进程来创建特定时间的时间表:
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
@[email protected], -- 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
@[email protected], -- 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
@[email protected] -- 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
[email protected][email protected], -- you can use this instead of the line below, if you change the proc parameter
@[email protected],
[email protected]_name = @newName -- if you want to change the schedule name
@enabled=1,
@freq_type=4, -- daily
@freq_interval=1, -- every day
@[email protected], -- eg. 2 = seconds
@[email protected], -- 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,而不是任务计划_(sp_delete_schedule可以得到其中任何一个)。
通知: 在更新和删除过程中,您可以使用名称或ID来识别计划。 尽管名称更加人性化,并且我使用它们来更容易遵循示例,但我强烈建议您使用ID。 名称并非必须是唯一的,所以如果碰巧创建了两个具有相同名称的调度,那么delete和update procs都会失败,除非将schedule_id用作参数。
@HumbleGrendel,使用应用程序用户可以更改现有的时间间隔,创建新的时间间隔并删除现有时间间隔。 –
每分钟执行一次SP是否存在问题?可能不会。对我来说这似乎是个好主意。 –
@ Nick.McDermaid,谢谢。我还有一个解决方案。也就是说,一旦应用程序用户更改了时间间隔,则时间表还会根据新时间间隔使用时间间隔表CRUD SP中的TSQL脚本创建/编辑/删除。我认为它比每分钟执行SP都要好。建议plz。 –