postgresql 执行存储过程时,未给出预期的输出

tcbh2hod  于 2023-11-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(163)
CREATE OR REPLACE PROCEDURE "internal"."get_last_weekday_of_month"(AsofDate TIMESTAMP_NTZ(9))
RETURNS VARCHAR(16777216)
LANGUAGE SQL
AS
$$
DECLARE
LastWeekDay TIMESTAMP_NTZ(9);
BEGIN
truncate table "internal"."monthend_process";
LastWeekDay := DATEADD(DAY,CASE DAYOFWEEK(AsofDate)
                               WHEN 6 THEN -1
                               WHEN 7 THEN -2
                               ELSE 0
                           END, AsofDate);

INSERT INTO "internal"."monthend_process" ("MonthEndDates","created_on", "RollOverStartDate",         "PrevPrevMonthEnd")
SELECT :LastWeekDay as "MonthEndDates", CURRENT_TIMESTAMP(), NULL,NULL
UNION
SELECT LAST_DAY(:AsofDate) as "MonthEndDates", CURRENT_TIMESTAMP(), NULL,NULL ;

UPDATE "internal"."monthend_process"
SET "RollOverStartDate" = DATEADD(DAY, -4, :AsofDate),
"PrevPrevMonthEnd" = LAST_DAY(DATEADD(MONTH, -1, :AsofDate));

  RETURN 'Stored procedure executed';
     END;
  $$;

字符串
这个存储过程工作正常,但是当我传递任何星期天的日期时,它不会返回星期五的日期,而对于星期六,它工作正常,它返回星期五的日期。

neekobn8

neekobn81#

我相信如果你改变以下内容:

LastWeekDay := DATEADD(DAY,CASE DAYOFWEEK(AsofDate)
                               WHEN 6 THEN -1
                               WHEN 7 THEN -2
                               ELSE 0
                           END, AsofDate);

字符串
下面它应该像你预期的那样工作:

LastWeekDay := DATEADD(DAY,CASE DAYOFWEEK(AsofDate)
                               WHEN 6 THEN -1
                               WHEN 0 THEN -2
                               ELSE 0
                           END, AsofDate);

相关问题