我正在学习Oracle Apex,Oracle SQL和PL / SQL,并且当前正在学习函数。我编写了一个函数,其中创建了一个表,存储了数据,并将该表返回给调用查询。问题是代码无法编译。我看不到问题出在哪里,并且已将代码与包括该站点在内的各种在线资源进行了比较。一切对我来说都很好,所以答案对我来说并不明显。
这是我写的代码:
CREATE OR REPLACE TYPE t_table AS OBJECT
(
futureValues NUMBER
);
CREATE OR REPLACE TYPE t_futureValues AS TABLE OF t_table;
/
CREATE OR REPLACE FUNCTION "CALCULATE_VALUE"( lastRowMinus0 IN NUMBER DEFAULT 1,
lastRowMinus1 IN NUMBER DEFAULT 2,
lastRowMinus2 IN NUMBER DEFAULT 3,
lastRowMinus3 IN NUMBER DEFAULT 4,
lastRowMinus4 IN NUMBER DEFAULT 5,
lastRowMinus5 IN NUMBER DEFAULT 6,
lastRowMinus6 IN NUMBER DEFAULT 7 )
RETURN t_futureValues AS
tableObject t_futureValues;
predictedValue NUMBER := 0;
lastRowMinus0Value NUMBER := 0;
lastRowMinus1Value NUMBER := 0;
lastRowMinus2Value NUMBER := 0;
lastRowMinus3Value NUMBER := 0;
lastRowMinus4Value NUMBER := 0;
lastRowMinus5Value NUMBER := 0;
lastRowMinus6Value NUMBER := 0;
avgDiff NUMBER := 0;
BEGIN
tableObject := t_futureValues();
lastRowMinus0Value := 3;
lastRowMinus1Value := 6;
lastRowMinus2Value := 9;
lastRowMinus3Value := 12;
lastRowMinus4Value := 14;
lastRowMinus5Value := 20;
lastRowMinus6Value := 60;
avgDiff := (lastRowMinus5Value - lastRowMinus6Value) + avgDiff;
avgDiff := (lastRowMinus4Value - lastRowMinus5Value) + avgDiff;
avgDiff := (lastRowMinus3Value - lastRowMinus4Value) + avgDiff;
avgDiff := (lastRowMinus2Value - lastRowMinus3Value) + avgDiff;
avgDiff := (lastRowMinus1Value - lastRowMinus2Value) + avgDiff;
avgDiff := (lastRowMinus0Value - lastRowMinus1Value) + avgDiff;
avgDiff := avgDiff / 6;
predictedValue := avgDiff + lastRowMinus0Value;
begin
for i in 2..13 loop
predictedValue := predictedValue + avgDiff;
IF predictedValue < 0 THEN
predictedValue := 0;
END IF;
insert into tableObject(futureValues)
values(predictedValue);
end loop;
end;
RETURN (tableObject);
END;
我收到的错误消息是:
ORA-06545:PL / SQL:编译错误-编译中止ORA-06550:第6行,第1列:PLS-00103:遇到符号“创建” ORA-06550:第0行,第0列:PLS -00565:必须完成T_TABLE作为潜在的REF目标(对象类型)
毫无疑问,问题很简单,所以如果有人知道,我将不胜感激。
答案 0 :(得分:2)
我不经常使用类型,但是您的插入内容正在尝试插入数组中。表必须是插入的目标。
使用以下代码:
CREATE OR REPLACE TYPE brianl.t_table AS OBJECT
(
futureValues NUMBER
);
CREATE OR REPLACE TYPE brianl.t_futureValues AS TABLE OF t_table;
/
CREATE OR REPLACE FUNCTION brianl."CALCULATE_VALUE"( lastRowMinus0 IN NUMBER DEFAULT 1,
lastRowMinus1 IN NUMBER DEFAULT 2,
lastRowMinus2 IN NUMBER DEFAULT 3,
lastRowMinus3 IN NUMBER DEFAULT 4,
lastRowMinus4 IN NUMBER DEFAULT 5,
lastRowMinus5 IN NUMBER DEFAULT 6,
lastRowMinus6 IN NUMBER DEFAULT 7 )
RETURN t_futureValues AS
tableObject t_futureValues;
predictedValue NUMBER := 0;
lastRowMinus0Value NUMBER := 0;
lastRowMinus1Value NUMBER := 0;
lastRowMinus2Value NUMBER := 0;
lastRowMinus3Value NUMBER := 0;
lastRowMinus4Value NUMBER := 0;
lastRowMinus5Value NUMBER := 0;
lastRowMinus6Value NUMBER := 0;
avgDiff NUMBER := 0;
BEGIN
tableObject := t_futureValues();
lastRowMinus0Value := 3;
lastRowMinus1Value := 6;
lastRowMinus2Value := 9;
lastRowMinus3Value := 12;
lastRowMinus4Value := 14;
lastRowMinus5Value := 20;
lastRowMinus6Value := 60;
avgDiff := (lastRowMinus5Value - lastRowMinus6Value) + avgDiff;
avgDiff := (lastRowMinus4Value - lastRowMinus5Value) + avgDiff;
avgDiff := (lastRowMinus3Value - lastRowMinus4Value) + avgDiff;
avgDiff := (lastRowMinus2Value - lastRowMinus3Value) + avgDiff;
avgDiff := (lastRowMinus1Value - lastRowMinus2Value) + avgDiff;
avgDiff := (lastRowMinus0Value - lastRowMinus1Value) + avgDiff;
avgDiff := avgDiff / 6;
predictedValue := avgDiff + lastRowMinus0Value;
begin
for i in 2..13 loop
predictedValue := predictedValue + avgDiff;
IF predictedValue < 0 THEN
predictedValue := 0;
END IF;
tableobject.extend();
tableobject(tableobject.count).futureValues := predictedValue;
-- insert into tableObject(futureValues)
-- values(predictedValue);
end loop;
end;
RETURN (tableObject);
END;