2016-04-15 37 views
6

在我的应用程序(ASP.NET,C#)中,我需要每天在一组预定义的时间间隔内运行一个存储过程。所以我创建了一个SQL工作并安排了相同的工作。但问题是,可以使用应用程序创建/修改此时间间隔,并将修改的时间间隔存储在表中。所以我需要在用户配置的时间间隔内运行存储过程。每天在用户配置的时间间隔内安排SQL作业

现在我正在执行以下步骤来解决问题。

  1. 创建一个作业来执行存储过程,并计划每1分钟为 。
  2. 在存储过程中,我将检查当前时间(分钟)和 预定的时间间隔。
  3. 如果匹配则TSQL代码部分存储过程 将执行的内部,其他明智跳过处理。

这工作正常,但存储过程将执行每一分钟(希望有人面临同样的问题)。

寻找更好的解决方案来解决这个问题。

+0

@HumbleGrendel,使用应用程序用户可以更改现有的时间间隔,创建新的时间间隔并删除现有时间间隔。 –

+0

每分钟执行一次SP是否存在问题?可能不会。对我来说这似乎是个好主意。 –

+0

@ Nick.McDermaid,谢谢。我还有一个解决方案。也就是说,一旦应用程序用户更改了时间间隔,则时间表还会根据新时间间隔使用时间间隔表CRUD SP中的TSQL脚本创建/编辑/删除。我认为它比每分钟执行SP都要好。建议plz。 –

回答

2

需要的第一件事是创建间隔计划的一些存储过程。

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用作参数。

4

假设这是不是频繁的事件,当表被更新执行sp_update_schedule。将其添加到更新过程中,或者直接更新表格作为触发器。

+0

比你@HumbleGrendel,使用应用程序的用户可以改变现有的时间间隔,创建新的时间间隔和删除现有的时间间隔。我需要按照指定的时间间隔每天安排这项工作(例如: - 每天下午8点,晚上10点,晚上11点30分)。我们可以使用execute sp_update_schedule来解决这个问题吗? –

2

我不确定您的应用程序或用户代码是如何工作的,但是您可以从用户代码触发SQL代理的触发器,以便通过调用https://msdn.microsoft.com/nl-nl/library/ms186757.aspx来启动该作业。唯一的限制是用户需要是该作业的所有者或sysadmin的成员,请参阅链接以获取更多详细信息。

+0

谢谢,但我需要安排这项工作每天在指定的时间间隔(例如: - 每天下午8点,晚上10点,晚上11点30分)。 –

+0

在这种情况下,为什么不附加3个时间表?或者我错过了什么? – Ako

+0

使用应用程序用户可以更改现有时间间隔,创建新间隔并删除现有时间间隔。我需要在应用程序用户配置的指定时间间隔内每天安排这项工作。此外,应用程序用户可以随时更改它,所以(我认为)一旦它由应用程序用户更改,则时间表还会根据新的时间间隔创建/编辑/删除。 –

3

1创建SQL作业,并创建第1步(给exec你的SP) https://msdn.microsoft.com/en-in/library/ms190268.aspx#Anchor_2

2。按照需求向作业添加多个计划(使用sp_add_jobschedule)。 细节:https://msdn.microsoft.com/en-us/library/ms366342.aspx

+0

注 如果您要求更新现有计划使用 sp_update_schedule(https://msdn.microsoft.com/en-us/library/ms187354.aspx) 如果您要求更新现有计划使用 sp_delete_schedule(HTTPS ://msdn.microsoft.com/en-us/library/ms175050.aspx) –

3

调度程序时间由应用程序管理是很好的。

但是在现实世界中,为什么用户会不断更新调度程序时间?我的意思是说频率。

所以我觉得只要时间是从应用程序修改再火这个新的存储过程将使用sp_update_schedule更新调度时间。

没有理由让存储过程每分钟执行一次。它只会在调度程序通过应用程序修改时触发。

相关问题