【问题标题】:Schedule SQL Job in a user configured time intervals everyday每天以用户配置的时间间隔安排 SQL 作业
【发布时间】:2016-08-06 21:44:46
【问题描述】:

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

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

  1. 创建了一个作业来执行存储过程并计划 每 1 分钟一次。
  2. 在存储过程中,我将检查当前时间(分钟)和 计划的时间间隔。
  3. 如果匹配,则存储过程中的 tsql 代码部分 将执行,否则跳过该过程。

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

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

【问题讨论】:

  • @HumbleGrendel ,使用应用程序用户可以更改现有的时间间隔,创建新的间隔和删除现有的间隔。
  • 每分钟执行一次SP有问题吗?可能不是。对我来说这似乎是个好主意。
  • @Nick.McDermaid,谢谢。我对此还有另一种解决方案。也就是说,一旦应用程序用户更改了时间间隔,那么时间表也会使用来自时间间隔表的 CRUD SP 内部的 TSQL 脚本基于新的时间间隔创建/编辑/删除。我认为这比每分钟都执行 SP 更好。建议请。
  • 如果用户仅通过您创建或可以更改的 SP[s] 修改您的时间间隔表 (TID),则此 SP[s] 是控制作业和调度哪个 TID 的最佳位置定义。
  • 也就是说,一旦应用程序用户更改了时间间隔,那么计划也会使用时间间隔表的 CRUD SP 内部的 TSQL 脚本根据新的时间间隔创建/编辑/删除。我认为这比每分钟都执行 SP 更好。 rgt?

标签: sql-server sql-server-2008 tsql jobs sql-job


【解决方案1】:

假设这不是一个频繁发生的事件,请在更新表时执行 sp_update_schedule。如果直接更新表,请将其添加到更新过程或作为触发器。

【讨论】:

  • 比你@HumbleGrendel ,使用应用程序用户可以更改现有的时间间隔,创建新的间隔和删除现有的间隔。我需要在指定的时间间隔内每天安排这项工作(例如: - 每天晚上 8 点、晚上 10 点、晚上 11:30)。我们可以使用 execute sp_update_schedule 解决这个问题吗?
【解决方案2】:

1 创建一个 sql 作业并创建第 1 步(执行您的 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.

【讨论】:

【解决方案3】:

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

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

所以我认为,每当从应用程序修改时间时,就会触发这个新的存储过程,它将使用 sp_update_schedule 更新调度程序时间。

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

【讨论】:

    【解决方案4】:

    首先需要一个用于创建间隔计划的存储过程。

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • 谢谢,但我需要在指定的时间间隔内每天安排这项工作(例如: - 每天晚上 8 点、晚上 10 点、晚上 11:30)。
      • 在这种情况下,为什么不只附加 3 个时间表?还是我错过了什么?
      • 使用应用程序用户可以更改现有的时间间隔,创建新的时间间隔和删除现有的时间间隔。我需要在应用程序用户配置的指定时间间隔内每天安排这项工作。应用程序用户也可以在这个时间间隔的任何时候更改它,所以(我认为)一旦应用程序用户更改了它,那么时间表也会根据新的时间间隔创建/编辑/删除。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 2018-10-18
      • 2021-07-04
      • 2017-11-17
      相关资源
      最近更新 更多