数据类型不一致:CAST REF?

时间:2011-12-03 10:06:57

标签: sql oracle

我有这个架构:

CREATE OR REPLACE TYPE tree_t
/

CREATE OR REPLACE TYPE element_t AS OBJECT (
  libelle varchar2(100),
  parent REF tree_t,
) NOT FINAL NOT INSTANTIABLE
/

CREATE OR REPLACE TYPE tree_t UNDER element_t()
/

CREATE OR REPLACE TYPE leaf_t UNDER element_t()
/

我正在尝试插入此表:

CREATE TABLE elements OF element_t;

对于没有父级的element_t,没问题:

INSERT INTO elements VALUES (tree_t('first', NULL));

但是有了父母:

INSERT INTO elements 
VALUES (tree_t('second', 
    (SELECT REF(e) 
     FROM elements e 
     WHERE e.libelle = 'first')));

我有这个结果:

ORA-00932: inconsistent datatypes: expected REF CINETREE.TREE_T 
got REF CINETREE.ELEMENT_T

我可以保留此架构以及如何进行插入吗? (与CAST?)

1 个答案:

答案 0 :(得分:2)

TREAT可以做到这一点:

INSERT INTO elements 
VALUES (tree_t('second', 
    (SELECT TREAT(REF(e) AS REF tree_t)
     FROM elements e 
     WHERE e.libelle = 'first')));