初始化对象类型构造函数oracle

时间:2017-07-09 15:06:37

标签: database oracle

我正在oracle中开发一个数据库。 我创建了一个超类型,答案和一个子类型,closed_answer。我添加了一个字段" type"区分它们,但我希望在构造函数中初始化此字段,以便在我在该表中插入元组时不插入此字段。我已经尝试了,但是当我在close_answer表中插入一个元组时,我必须指定类型,但我不应该。我哪里错了?

create type answertyp as object(

id integer,

text varchar2(50),

type varchar2(25),

constructor function answertyp(self in out nocopy answertyp, text varchar2) return self as result) not final;

create type body answertyp is

constructor function answertyp(self in out nocopy answertyp, text varchar2) return self as result is

begin

self.text := text;

self.type:= 'answer';

return;

end;

end;
/

create type closed_answertyp under closed_answer(

constructor function closed_answertyp(self in out nocopy closed_answertyp, text varchar2) return self as result
) final;

create type body closed_answertyp is

constructor function closed_answertyp (self in out nocopy closed_answertyp, text varchar2) return self as result is

begin

self.text := text;

self.type := 'closed_answer';

return;

end;

end;

现在,如果我尝试这个查询,它会说参数的数量是错误的。有帮助吗?感谢

insert into answer select closed_answertyp(1, 'ten') from dual;

1 个答案:

答案 0 :(得分:0)

SQL语句中的参数与构造函数中的参数不匹配。

id添加到构造函数:

constructor function closed_answertyp (self in out nocopy closed_answertyp, id integer, text varchar2) return self as result is

或者从SQL语句中删除id

insert into answer select closed_answertyp('ten') from dual;