在我的应用程序(ASP.NET,C#)中,我需要每天在一组预定义的时间间隔内运行存储过程.所以我创建了一个sql作业并安排了相同的工作.但问题是,可以选择使用应用程序创建/修改此时间间隔,这会将修改后的时间间隔存储在表中.所以我需要在用户配置的时间间隔内运行存储过程.
现在我正在执行以下步骤来解决此问题.
>创建了一个执行存储过程和计划的作业
每1分钟一次.
>在存储过程中,我将检查当前时间(分钟)和
预定的间隔.
>如果匹配则存储过程内的tsql代码部分
将执行,否则跳过该过程.
这工作正常,但存储过程将每分钟执行一次(希望有人遇到同样的问题).
解决方法
首先需要的是一个用于创建间隔时间表的小存储过程.
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',-- every day @freq_subday_type=1,-- At the specified time @freq_subday_interval=1,-- once a day,probably not used @freq_relative_interval=0,or specify some valid date @active_start_time=@StartTime,-- 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),'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,as it might couse some errrors @active_end_time=235959 -- active to 23:59:59 END; GO
用法:
EXEC spUpdateSchedule_Interval @scheduleName = 'UserB_Schedule',@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作为参数.