2

から値を取得できる 2 つのパラメーターを使用して指定できるジョブの次の月次実行時間を計算する必要があります。

パラメータ 1:
日曜日は 1、月曜日は 2、火曜日は 3、水曜日は 4、木曜日は 5、金曜日は 6、土曜日は 7、曜日は 8、平日は 9、週末は 10

パラメータ 2:
1 番目は 1、2 番目は 2、3 番目は 4、4 番目は 8、最後は 16

これら 2 つのパラメーターに基づいて、月の最初の平日や月の最後の日曜日などの実行時間を指定できます。

現在の日付、para1およびpara2を知るストアドプロシージャを使用して、この日付に到達するにはどうすればよいですか

4

2 に答える 2

1
create function schema.get_next_run_date(@day int, @week int)
returns datetime
as
begin
declare @rtResult datetime
declare @frstDayOfMonth datetime
set @frstDayOfMonth = SELECT TRUNC(current_date, 'MM') FROM dual
if @day <= 7
    set @rtResult = SELECT NEXT_DAY(@frstDayOfMonth + ((parm2-1)*7) day, param1) "NEXT DAY" FROM DUAL
else if @day = 8
    set @rtResult = SELECT NEXT_DAY(@frstDayOfMonth + pamr2) "NEXT DAY" FROM DUAL
else if @day = 9
    declare intDay int
    set @intDay = datepart(weekday, @frstDayOfMonth)
    if (@intDay between 2 and 6)
        set @rtResult = @frstDayOfMonth
    else if (@intDay = 1)
        set @rtResult = @frstDayOfMonth + 1 day
    else
        set @rtResult = @frstDayOfMonth + 2 day
else if @day = 10
    declare intDay int
    set @intDay = datepart(weekday, @frstDayOfMonth)
    if (@intDay between 2 and 6)
        set @rtResult = @frstDayOfMonth + 7 - @intDay
    else
        set @rtResult = @frstDayOfMonth
else
    set @rtResult = null
return @rtResult
end
go

未検証。しかし、これがお役に立てば幸いです。また、日付がすでに遅れている場合は、nullを返したい場合があります。

于 2012-02-24T11:53:25.590 に答える
1

これで始められるかもしれません。出典:月の N 番目の平日を取得する方法

CREATE FUNCTION dbo.fnGetNthWeekdayOfMonth
(
 @theDate DATETIME,
 @theWeekday TINYINT,
 @theNth SMALLINT
)
RETURNS DATETIME
BEGIN
  RETURN  (
            SELECT  theDate
            FROM    (
                        SELECT  DATEADD(DAY, 7 * @theNth - 7 * SIGN(SIGN(@theNth) + 1) +(@theWeekday + 6 - DATEDIFF(DAY, '17530101', DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) % 7) % 7, DATEADD(MONTH, DATEDIFF(MONTH, @theNth, @theDate), '19000101')) AS theDate
                        WHERE   @theWeekday BETWEEN 1 AND 7
                                AND @theNth IN (-5, -4, -3, -2, -1, 1, 2, 3, 4, 5)
                    ) AS d
            WHERE   DATEDIFF(MONTH, theDate, @theDate) = 0
        )
END

それはまさにあなたが探しているものではありませんが、主要部分をカバーする必要があります.

于 2012-02-24T12:29:51.400 に答える