我创建了一个包含这样功能的包:
create or replace
PACKAGE TRANSAC_ERRONNEES AS
TYPE dateArrayVar IS TABLE OF DATE;
FUNCTION calc_date_moyenne(dateArrayIn dateArrayVar
) RETURN DATE;
END TRANSAC_ERRONNEES;
身体:
CREATE OR REPLACE PACKAGE BODY TRANSAC_ERRONNEES AS
FUNCTION calc_date_moyenne(dateArrayIn datearrayvar) RETURN DATE IS
dateFinal DATE;
tempsEnSeconde NUMBER;
tempsMoyenEnSeconde NUMBER;
BEGIN
tempsEnSeconde := 0;
tempsMoyenEnSeconde := 0;
FOR i IN 1..dateArrayIn.count loop
tempsEnSeconde := to_number(to_char(dateArrayIn(i), 'HH24')) * 60 * 60 + to_number(to_char(dateArrayIn(i), 'MI')) * 60 + to_number(to_char(dateArrayIn(i), 'SS'));
tempsMoyenEnSeconde := tempsMoyenEnSeconde+tempsEnSeconde;
end loop;
tempsMoyenEnSeconde := tempsMoyenEnSeconde/dateArrayIn.count;
dateFinal := to_date(TO_CHAR(dateArrayIn(1),'DD-MM-YYYY') || ' ' || TO_CHAR(to_date(tempsMoyenEnSeconde,'sssss'), 'HH24:MI:SS'), 'DD-MM-YYYY HH24:MI:SS');
dbms_output.put_line(TO_CHAR(dateFinal,'DD-MM-YYYY HH24:MI:SS'));
RETURN dateFinal;
END calc_date_moyenne;
END TRANSAC_ERRONNEES;
/
我尝试像这样测试这个函数:
DECLARE
dates dateArrayVar;
resultat DATE;
BEGIN
dates := dateArrayVar(SYSDATE, SYSDATE);
resultat := transac_erronnees.calc_date_moyenne(dates);
END;
我收到以下错误:
PLS-00320:此表达式类型的声明是 不完整或畸形
请告诉我错误。提前谢谢。
答案 0 :(得分:0)
您使用的方式不正确。您需要使用带extend
的数组来分配内存,然后在其中指定值。
DECLARE
dates dateArrayVar;
resultat DATE;
BEGIN
dates.extend(2); -- Since you want to assign two values, extend by 2
dates(1) := SYSDATE;
dates(2) := SYSDATE;
resultat := transac_erronnees.calc_date_moyenne(dates);
END;
更新:第二件事是DateArrayVar只在你创建的包中有范围,在匿名块中,Oracle无法看到该声明。一种解决方案是创建对所有其他过程和匿名块可见的外部类型,或者进行测试,在包中创建一个过程并从外面调用它。
create or replace
PACKAGE TRANSAC_ERRONNEES AS
TYPE dateArrayVar IS TABLE OF DATE;
FUNCTION calc_date_moyenne(dateArrayIn dateArrayVar
) RETURN DATE;
PROCEDURE test;
END TRANSAC_ERRONNEES;
/
CREATE OR REPLACE PACKAGE BODY TRANSAC_ERRONNEES AS
FUNCTION calc_date_moyenne(dateArrayIn datearrayvar) RETURN DATE IS
dateFinal DATE;
tempsEnSeconde NUMBER;
tempsMoyenEnSeconde NUMBER;
BEGIN
tempsEnSeconde := 0;
tempsMoyenEnSeconde := 0;
FOR i IN 1..dateArrayIn.count loop
tempsEnSeconde := to_number(to_char(dateArrayIn(i), 'HH24')) * 60 * 60 + to_number(to_char(dateArrayIn(i), 'MI')) * 60 + to_number(to_char(dateArrayIn(i), 'SS'));
tempsMoyenEnSeconde := tempsMoyenEnSeconde+tempsEnSeconde;
end loop;
tempsMoyenEnSeconde := tempsMoyenEnSeconde/dateArrayIn.count;
dateFinal := to_date(TO_CHAR(dateArrayIn(1),'DD-MM-YYYY') || ' ' || TO_CHAR(to_date(tempsMoyenEnSeconde,'sssss'), 'HH24:MI:SS'), 'DD-MM-YYYY HH24:MI:SS');
dbms_output.put_line(TO_CHAR(dateFinal,'DD-MM-YYYY HH24:MI:SS'));
RETURN dateFinal;
END calc_date_moyenne;
PROCEDURE test
dates dateArrayVar;
resultat DATE;
BEGIN
dates.extend(2); -- Since you want to assign two values, extend by 2
dates(1) := SYSDATE;
dates(2) := SYSDATE;
resultat := transac_erronnees.calc_date_moyenne(dates);
END;
END TRANSAC_ERRONNEES;
/
要测试,请将其命名为
BEGIN
TRANSAC_ERRONNEES.TEST;
END;
/
答案 1 :(得分:0)
DateArrayVar仅在包
中具有范围
我找到了解决方案:我必须指定包:TRANSAC_ERRONNEES.dateArrayVar;
所以:
DECLARE
dates TRANSAC_ERRONNEES.dateArrayVar;
resultat DATE;
BEGIN
dates := TRANSAC_ERRONNEES.dateArrayVar(SYSDATE, SYSDATE);
resultat := transac_erronnees.calc_date_moyenne(dates);
dbms_output.put_line(TO_CHAR(resultat,'DD-MM-YYYY HH24:MI:SS'));
END;
它现在有效,谢谢