Search code examples
sqloracleplsqldereferencevarray

Oracle PL/SQL: How to DEREF from a VARRAY of REFs?


I'm new to Oracle Objects and I have a problem. I don't know how to dereference an item from a VARRAY of REFs. Below is some source code that reproduces the problem that I have. The error is: PLS-00306: Wrong number or types of arguments in call to 'DEREF'


DROP TYPE LOC FORCE
/
DROP TYPE LIST_LOC FORCE
/
DROP TYPE PIZ FORCE
/

CREATE OR REPLACE TYPE LOC AS OBJECT(
  NAME      VARCHAR2(30),
  MEMBER FUNCTION GET_NAME RETURN VARCHAR2 
)
/

CREATE OR REPLACE TYPE BODY LOC AS
  MEMBER FUNCTION GET_NAME RETURN VARCHAR2 IS
  BEGIN
    RETURN SELF.NAME;
  END;
END;
/

CREATE OR REPLACE TYPE LIST_LOC AS VARRAY(10) OF REF LOC
/

CREATE OR REPLACE TYPE PIZ AS OBJECT(
  LOCS      LIST_LOC,
  MEMBER PROCEDURE DISPLAY_LOCS
)
/

CREATE OR REPLACE TYPE BODY PIZ AS
  MEMBER PROCEDURE DISPLAY_LOCS IS
  BEGIN
    FOR IDX IN SELF.LOCS.FIRST..SELF.LOCS.LAST LOOP
      DBMS_OUTPUT.PUT_LINE(DEREF(SELF.LOCS(IDX)).GET_NAME()); --this is the line that generates the error
    END LOOP;
  END;
END;
/

The error appears in DISPLAY_LOCS procedure, when I try to get the REF at position IDX from the LOCS varray, and to DEREF it to get the name.


Solution

  • the DEREF must be in a SQL Statement: http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28371/adobjbas.htm#i463707

    In PL/SQL the VALUE, REF and DEREF functions can appear only in a SQL statement

    this works

    CREATE OR REPLACE TYPE BODY PIZ AS
      MEMBER PROCEDURE DISPLAY_LOCS IS
      x varchar2(30) ;
      BEGIN
        FOR IDX IN SELF.LOCS.FIRST..SELF.LOCS.LAST LOOP
            select DEREF(SELF.LOCS(IDX)).GET_NAME() into x from dual ;
          DBMS_OUTPUT.PUT_LINE(x); --this is the line that generates the error
        END LOOP;
      END;
    END;
    /
    

    nice test case to reproduce!