在Oracle数据库对象的构造函数中初始化REF

时间:2017-01-17 09:31:13

标签: oracle object

我在Oracle中初始化REF值时遇到问题。当我尝试在构造函数中设置它们时,无论我如何尝试它都会出错。有没有办法在这样的情况下建造他们?

CREATE TYPE tSomething AS OBJECT (
 Name Varchar(30)
);

CREATE TYPE tSomethingElse AS OBJECT (
 Something REF tSomething
);

1 个答案:

答案 0 :(得分:0)

Ref是现有对象的唯一标识符。您必须创建对象表以获取此唯一标识符。

CREATE TYPE tSomething AS OBJECT (
 Name Varchar(30)
);


create table tSomething_table of tSomething;


CREATE TYPE tSomethingElse AS OBJECT (
 Something REF tSomething
);



declare 
 tSomethingElse_obj tSomethingElse; 
 tSomething_ref REF tSomething;
 tSomething_obj tSomething;
begin
 -- insert objects into tSomething_table
 insert into tSomething_table values('abc');
 insert into tSomething_table values('yyy');
 -- get ref one object. 
 select ref(p) into tSomething_ref  from tSomething_table p where p.name = 'abc';
 -- initialize tSomethingElse_obj; 
 tSomethingElse_obj := new tSomethingElse(tSomething_ref);

 -- extract get real tSomething object from ref
 select deref(tSomething_ref) into tSomething_obj  from dual;

end;