Schedule SQL 每天在用户配置的时间间隔内执行作业
Schedule SQL Job in a user configured time intervals everyday
在我的应用程序 (ASP.NET, C#) 中,我需要每天在一组预定义的时间间隔内 运行 一个存储过程。所以我创建了一个 sql 作业并安排了相同的作业。但问题是,使用应用程序可以选择 create/modify 这个时间间隔,这会将修改后的时间间隔存储在 table 中。所以我需要在用户配置的时间间隔内运行存储过程。
现在我正在执行以下步骤来解决问题。
- 创建了一个作业来执行存储过程并计划
每 1 分钟
- 在存储过程中,我将检查当前时间(分钟)和
计划的间隔。
- 如果匹配则存储过程中的 tsql 代码部分
将执行,否则跳过该过程。
这工作正常,但存储过程将每分钟执行一次(希望有人遇到同样的问题)。
寻找更好的解决方案来解决这个问题。
假设这不是经常发生的事件,请在 table 更新时执行 sp_update_schedule。如果直接更新 table.
,则将其添加到更新过程或作为触发器
我不确定您的应用程序或用户代码是如何工作的,但是您可以从您的用户代码触发 SQL 代理的触发器,通过调用 https://msdn.microsoft.com/nl-nl/library/ms186757.aspx 来启动作业。唯一的限制是用户需要是作业的所有者或系统管理员的成员,请参阅 link 了解更多详细信息。
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.
调度程序时间由应用程序管理很好。
但在现实世界中,为什么用户会不断更新调度程序时间?我是说频率。
所以我认为每当从应用程序修改时间时,都会触发这个新的存储过程,它将使用 sp_update_schedule
更新调度程序时间。
存储过程没有理由每分钟执行一次。它只会在通过应用程序修改调度程序时触发。
首先需要的是一个用于创建间隔计划的小存储过程。
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 作为参数。
在我的应用程序 (ASP.NET, C#) 中,我需要每天在一组预定义的时间间隔内 运行 一个存储过程。所以我创建了一个 sql 作业并安排了相同的作业。但问题是,使用应用程序可以选择 create/modify 这个时间间隔,这会将修改后的时间间隔存储在 table 中。所以我需要在用户配置的时间间隔内运行存储过程。
现在我正在执行以下步骤来解决问题。
- 创建了一个作业来执行存储过程并计划 每 1 分钟
- 在存储过程中,我将检查当前时间(分钟)和 计划的间隔。
- 如果匹配则存储过程中的 tsql 代码部分 将执行,否则跳过该过程。
这工作正常,但存储过程将每分钟执行一次(希望有人遇到同样的问题)。
寻找更好的解决方案来解决这个问题。
假设这不是经常发生的事件,请在 table 更新时执行 sp_update_schedule。如果直接更新 table.
,则将其添加到更新过程或作为触发器我不确定您的应用程序或用户代码是如何工作的,但是您可以从您的用户代码触发 SQL 代理的触发器,通过调用 https://msdn.microsoft.com/nl-nl/library/ms186757.aspx 来启动作业。唯一的限制是用户需要是作业的所有者或系统管理员的成员,请参阅 link 了解更多详细信息。
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.
调度程序时间由应用程序管理很好。
但在现实世界中,为什么用户会不断更新调度程序时间?我是说频率。
所以我认为每当从应用程序修改时间时,都会触发这个新的存储过程,它将使用 sp_update_schedule
更新调度程序时间。
存储过程没有理由每分钟执行一次。它只会在通过应用程序修改调度程序时触发。
首先需要的是一个用于创建间隔计划的小存储过程。
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 作为参数。