我正在尝试从所需日期开始到当月的月份清单,然后我制作了这个包裹:
CREATE OR REPLACE PACKAGE DATE_EXTENSIONS IS
TYPE MONTH_RECORD IS RECORD (
MONTH_YEAR DATE,
MONTH_INDEX NUMBER
);
TYPE MONTH_TABLE IS TABLE OF MONTH_RECORD;
FUNCTION GET_DATE RETURN DATE;
FUNCTION GET_MONTHS(D IN DATE) RETURN MONTH_TABLE;
END;
CREATE OR REPLACE PACKAGE BODY DATE_EXTENSIONS IS
FUNCTION GET_DATE RETURN DATE IS
BEGIN
RETURN(SYSDATE);
END GET_DATE;
FUNCTION GET_MONTHS(D IN DATE) RETURN MONTH_TABLE IS
T MONTH_TABLE;
BEGIN
SELECT ADD_MONTHS(D, ROWNUM - 1) AS MONTH_YEAR,
ROWNUM AS MONTH_INDEX BULK COLLECT
INTO T
FROM DUAL
WHERE MONTHS_BETWEEN(GET_DATE(), D) >= 0
CONNECT BY LEVEL <= ROUND(MONTHS_BETWEEN(GET_DATE(), D));
RETURN T;
END;
END;
查询工作正常,您可以在此处看到:
SELECT ADD_MONTHS(SYSDATE - 300, ROWNUM - 1) AS MONTH_YEAR, ROWNUM AS R
FROM DUAL
WHERE MONTHS_BETWEEN(SYSDATE, SYSDATE - 300) >= 0
CONNECT BY LEVEL <= ROUND(MONTHS_BETWEEN(SYSDATE, SYSDATE - 300))
但是当我尝试封装在包中时,我收到了ORA-00902错误。顺便说一句,GET_DATE()
函数用于测试目的。