oracle中的嵌套表给出错误

时间:2013-04-24 09:30:08

标签: oracle oracle11g

我无法创建嵌套表。我执行了以下命令:

create or replace type address_ty as object(
    street nvarchar2(15),
    city nvarchar2(15),
    district nvarchar2(15));

create or replace type name_ty as object(
    name nvarchar2(15),
    address address_ty);

create or replace type dependent_ty as object(
    relation nvarchar2(15),
    name name_ty,
    age number);

create or replace type dependent_list as table of dependent_ty;

create or replace type employee_info_ty as object(
    emp_id nvarchar(15),
    name name_ty,
    salary nvarchar(15),
    dept_id nvarchar(15),
    dependents dependent_list);

当我创建表时,它会出错:

create table employee_info of employee_info_ty OIDINDEX OID_EMPLOYEE_INFO
nested table dependent store as dependent_ty;

SQL Error: ORA-00902: invalid datatype
00902. 00000 -  "invalid datatype"

1 个答案:

答案 0 :(得分:1)

您的上一个类型声明无效:

create or replace type employee_info_ty as object(
    emp_id nvarchar(15),
    name name_ty,
    salary nvarchar(15),
    dept_id nvarchar(15),
    dependents dependent_list);
/

Warning: Type created with compilation errors.

show errors

Errors for TYPE EMPLOYEE_INFO_TY:

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0      PL/SQL: Compilation unit analysis terminated
2/12     PLS-00201: identifier 'NVARCHAR' must be declared

如果更改为使用nvarchar2,则针对invalid datatype的{​​{1}}错误会消失但会被其他错误替换:

employee_info_ty

...这是因为您在类型定义中调用了嵌套表create or replace type employee_info_ty as object( emp_id nvarchar2(15), name name_ty, salary nvarchar2(15), dept_id nvarchar2(15), dependents dependent_list); / Type created. create table employee_info of employee_info_ty OIDINDEX OID_EMPLOYEE_INFO nested table dependents store as dependent_ty; nested table dependent store as dependent_ty * ERROR at line 2: ORA-00904: : invalid identifier ,但此处调用了dependents。您也不能将dependent重用dependent_ty子句,因为这是您刚刚创建的对象类型;你会得到一个ORA-00955。这有效:

store as

从评论中的问题中,如果您填充如下行:

create table employee_info of employee_info_ty OIDINDEX OID_EMPLOYEE_INFO
nested table dependents store as dependents_tab;

Table created.

您可以选择以下属性:

insert into employee_info values (
    employee_info_ty('1', name_ty('Joe Bloggs',
            address_ty('1 Main Street', 'Omaha', 'Nebraska')),
        '20000', '2', dependent_list(dependent_ty('Daughter',
            name_ty('Emily Bloggs',
                address_ty('1 Main Street', 'Omaha', 'Nebraska')),
            '14')
        )
    )
);

嵌套属性如:

select e.emp_id, e.name.name
from employee_info e;

EMP_ID          NAME.NAME
--------------- ---------------
1               Joe Bloggs

我想你在PL / SQL中操纵它们,但原理是一样的。

不确定为什么要将select d.relation, d.name.name, d.age from the(select e.dependents from employee_info e where emp_id = '1') d; RELATION NAME.NAME AGE --------------- --------------- ---------- Daughter Emily Bloggs 14 emp_id存储为字符串。