我正在尝试在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 doc
,它应该有效感谢。
答案 0 :(得分:4)
将DEREF转换为SQL。 e.g。
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>
答案 1 :(得分:0)
尝试缩小问题的一些想法:
DEREF
与.Att2
分开并测试以查看它不喜欢的脚本部分;的也强> att
类型以查看它是否返回任何内容。DEREF
是否适用于它;的也强> REF(TheType.Att2)
是否在创建类型之外工作。我确实同意你,虽然在第一个例子中似乎没有问题。
祝你好运。答案 2 :(得分: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
在创建对象类型时起作用。它用于返回对象的实例值。