0

我有以下 SQL 脚本:

PROCEDURE "P_OVERVIEW" ()
LANGUAGE SQLSCRIPT
READS SQL DATA
AS
BEGIN
  DECLARE I INT = 0;
  WHILE :I < 24 DO
    SELECT A, B, COUNT(A) AS COUNT, LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) AS "DATE", I
    FROM Table
    WHERE LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) BETWEEN ENTRY_DATE AND VALID_UNTIL
    GROUP BY A, B
    ORDER BY B, A;
    I := I+1;
  END WHILE;
END;

我在 SAP Cloud Platform 上的 .hdbprocedure-File 中使用它。部署工作正常。当我用 调用该过程时,CALL P_OVERVIEW()我只是在I = 0. 发生这种情况的任何想法?

4

1 回答 1

0

代码中有一个小错字。
引用变量的值时,I必须:在前面放一个冒号::I

以下应该工作

PROCEDURE "P_OVERVIEW" ()
LANGUAGE SQLSCRIPT
READS SQL DATA
AS
BEGIN
  DECLARE I INT = 0;
  WHILE :I < 24 DO
    SELECT A, B, COUNT(A) AS COUNT, LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) AS "DATE", I
    FROM Table
    WHERE LAST_DAY(ADD_MONTHS(CURRENT_TIMESTAMP, -:I)) BETWEEN ENTRY_DATE AND VALID_UNTIL
    GROUP BY A, B
    ORDER BY B, A;
    I := :I+1;
  END WHILE;
END;

备注:在这里使用循环可能不是最好的方法。您可以创建一个子查询,即时生成过去两年的“本月最后一天”。生成器SERIES_GENERATE_表函数是一个简单的选择。

于 2020-02-04T02:06:12.967 回答