Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Why won't this PL/SQL function compile?

3988598Jul 30 2019 — edited Jul 31 2019

Hi,

I'm learning about Oracle Apex, Oracle SQL and PL/SQL and I'm currently learning about functions.  I have written a function in which a table is created, data is stored and the table is returned to the calling query.  The problem is the code won't compile.  I can't see where the problem may be and I've compared the code to various online resources including this site.  Everything looks fine to me, so the answer is not obvious to me.

This is the code I wrote:

    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;

The error message I get is:

ORA-06545: PL&#x2F;SQL: compilation error - compilation aborted ORA-06550: line 6, column 1: PLS-00103: Encountered the symbol &quot;CREATE&quot; ORA-06550: line 0, column 0: PLS-00565: T_TABLE must be completed as a potential REF target (object type)

I have no doubt that the problem is something simple, so if anyone knows, then I'd be grateful.

Comments

Processing

Post Details

Added on Jul 30 2019
14 comments
2,120 views