我有一个包含CRON表达式的列,该表达式表示用户执行任务的频率。我想构建一个视图,列出给定用户的日期和事情。但我需要在T-SQL中的日期时间计算我的下一个CRON事件。如何在SQL中解释我的CRON表达式?
例如: 列值= [0 30 8 1 *?]
我会写: SELECT CrontabSchedule('0 30 8 1 *?',GETDATE())FROM dbo.UserTasks
有人有解决方案吗?
答案 0 :(得分:0)
也许这个链接可以给你一个提示:https://ask.sqlservercentral.com/questions/117285/tsql-sql-function-for-a-cron-expression.html
链接中显示的UDF如下:
CREATE PROCEDURE Q117285.CreateJobScheduleFromCronExpression
@CronExpression varchar(200)
, @FieldDelimiterCharacter char(1) -- any whitespace character
AS
BEGIN;
DECLARE @CronExpressionArray TABLE
(
Seconds varchar(50)
, Minutes varchar(50)
, Hours varchar(50)
, DayOfMonth varchar(50)
, Month varchar(50)
, DayOfWeek varchar(50)
, Year varchar(50)
);
INSERT @CronExpressionArray
SELECT
[Seconds] = [1]
, [Minutes] = [2]
, [Hours] = [3]
, [DayOfMonth] = [4]
, [Month] = [5]
, [DayOfWeek] = [6]
, [Year] = [7]
FROM
(
SELECT ds.ItemNumber, ds.Item
FROM dbo.DelimitedSplit8K(@CronExpression, @FieldDelimiterCharacter) ds
) pSrc
PIVOT
(
MAX(pSrc.Item)
FOR pSrc.ItemNumber IN
([1], [2], [3], [4], [5], [6], [7])
) pvt
;
SELECT * FROM @CronExpressionArray;
END;
DelimitedSplit8K UDF摘自文章:http://www.sqlservercentral.com/articles/Tally+Table/72993/
CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
(@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
-- enough to cover VARCHAR(8000)
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
SELECT 1 UNION ALL
SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
SELECT s.N1,
ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
FROM cteStart s
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
Item = SUBSTRING(@pString, l.N1, l.L1)
FROM cteLen l
;
答案 1 :(得分:0)
SQL Server中没有内置函数来生成基于Cron表达式的下一个日期。唯一的方法是,在C#中实现cron exprssions并将其生成为dll,然后使用SQL Server CLR注册DLL。
https://github.com/atifaziz/NCrontab/wiki/SQL-Server-Crontab
答案 2 :(得分:0)
我需要做类似的事情,最终建立自己的tSQLcron
当前它不是基于SET的,因为它是存储过程。用法示例是
DECLARE @out_is_cron_true BIT ;
EXEC tSQLcron.usp_is_date_in_cron_period
@cron_expression = N'* 0/15 * * * *' -- nvarchar(100)
, @validate_date = '2020-01-01 13:15:00' -- datetime
, @out_is_cron_true = @out_is_cron_true OUTPUT -- bit
IF (@out_is_cron_true = 1 )
BEGIN
PRINT 'DO SOMETHING';
END