4

我正在尝试在 Oracle 中创建一个对象方法,如下所示:

CREATE OR REPLACE TYPE BODY TheType AS
    MEMBER FUNCTION getAtt RETURN VARCHAR2 IS
    BEGIN
        RETURN DEREF(SELF.Att).Att2;
    END;
END;
/

但我收到以下错误:

PLS-00306: wrong number or types of arguments in call to 'DEREF'

TheType 类型也是这样声明的:

CREATE OR REPLACE TYPE TheType UNDER SuperType ();
/

...

ALTER TYPE TheType ADD ATTRIBUTE ( Att REF TheType ) CASCADE;

...

ALTER TYPE TheType 
    ADD MEMBER FUNCTION getAtt RETURN VARCHAR2
CASCADE;

以及 Supertype 的定义:

CREATE OR REPLACE TYPE SuperType AS OBJECT ( Att2 VARCHAR2(50) )
NOT FINAL NOT INSTANTIABLE;
/

我给 DEREF 函数一个类型正确的 var,为什么会出现这个错误?

如果我信任Oracle 文档,它应该可以工作

谢谢。

4

3 回答 3

4

将 DEREF 转换为 SQL。例如

SQL> create or replace type supertype as object ( att2 varchar2(50) )
  2  not final not instantiable;
  3  /

Type created.

SQL> create or replace type thetype under supertype (
  2  att ref thetype,
  3  member function getatt return varchar2);
  4  /

Type created.

SQL> show errors type thetype
No errors.
SQL> create or replace type body thetype as
  2    member function getatt return varchar2 is
  3      v_t thetype;
  4    begin
  5             select deref(self.att) into v_t from dual;
  6             return v_t.att2;
  7    end;
  8  end;
  9  /

Type body created.

SQL> show errors type body thetype
No errors.
SQL> create table thetypes of thetype;

Table created.

SQL> insert into thetypes values ('hi there', null);

1 row created.

SQL> set serverout on
SQL> declare
  2    v_t thetype;
  3  begin
  4    select thetype(null, ref(a)) into v_t from thetypes a;
  5    dbms_output.put_line(v_t.getatt);
  6  end;
  7  /
hi there

PL/SQL procedure successfully completed.

SQL>
于 2012-12-26T17:30:48.137 回答
0

尝试缩小问题范围的一些想法:

  1. DEREF将 the与 the分开.Att2并测试以查看它不喜欢脚本的哪个部分;
  2. att使用独立的 pl/sql尝试该类型,看看它是否返回任何内容。
  3. 如果它确实看到它是否DEREF有效,那么;
  4. 查看是否REF(TheType.Att2)在独立的 pl/sql 中创建类型之外的工作。

我同意你的看法,虽然一开始似乎没有问题。

祝你好运。

于 2012-12-26T09:58:37.300 回答
0

我们可以将 REF 声明为变量、参数、字段或属性。

DECLARE
  emp         employee_typ;
  emp_ref REF employee_typ;  -- creating one more type with REF
BEGIN
  SELECT REF(e) INTO emp_ref FROM employee_tab e WHERE e.employee_id = 370;
  UPDATE employee_tab e 
    SET e.address = address_typ('NIZAM', 'Hyd', 'AP', '465876')
    WHERE REF(e) = emp_ref;
END;

我在创建对象类型时不思考REF和工作。DEREF它用于返回对象的实例值。

于 2012-12-26T10:14:31.890 回答