每天在用户配置的时间间隔内安排SQL作业

Abd*_*eed 6 t-sql sql-server jobs sql-server-2008 sql-job

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

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

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

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

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

小智 3

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

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
Run Code Online (Sandbox Code Playgroud)

用法示例:

DECLARE @ScheduleId int;
    EXEC spCreateSchedule_Interval 
        @scheduleName = 'UserA_Schedule',
        @intervalType = 'minutes',
        @interval = 27,
        @ScheduleId = @ScheduleId OUT;
Run Code Online (Sandbox Code Playgroud)

这应该创建每 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
        @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
Run Code Online (Sandbox Code Playgroud)

用法示例:

DECLARE @ScheduleId INT;
    EXEC spCreateSchedule_ExactTime 
        @scheduleName = 'UserB_Schedule',
        @timeToRun = '14:58:00',
        @ScheduleId = @ScheduleId OUT;
Run Code Online (Sandbox Code Playgroud)

这应该创建每天 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
Run Code Online (Sandbox Code Playgroud)

以及用法:

EXEC spUpdateSchedule_Interval 
    @scheduleName = 'UserB_Schedule',
    @intervalType = 'minutes',
    @interval = 25;
GO
Run Code Online (Sandbox Code Playgroud)

您现在应该能够通过类推创建 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
Run Code Online (Sandbox Code Playgroud)

及其用法:

USE msdb
GO 

EXEC spDeleteSchedule 'UserA_Schedule';
Run Code Online (Sandbox Code Playgroud)

或者您可以轻松编写替代方案,使用schedule_id而不是schedule_name(sp_delete_schedule可以获取其中任何一个)。

注意: 在更新和删除过程中,您可以使用名称或 ID 来识别计划。虽然名称更人性化,而且我使用它们作为示例是为了更容易理解,但我强烈建议您使用 ID。名称不强制是唯一的,因此如果您碰巧创建两个具有相同名称的计划,那么删除和更新过程都会失败,除非您使用 Schedule_id 作为参数。