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

早过忘川 提交于 2019-12-12 21:41:05

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/5860984/oracle-pl-sql-how-to-deref-from-a-varray-of-refs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!