您的最后一个类型声明无效:
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,则针对employee_info_ty 的invalid datatype 错误消失,但被另一个错误取代:
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 重新用于store as 子句,因为这是您刚刚创建的对象类型;你会得到一个 ORA-00955。这有效:
create table employee_info of employee_info_ty OIDINDEX OID_EMPLOYEE_INFO
nested table dependents store as dependents_tab;
Table created.
来自 cmets 中的问题,如果您填充如下一行:
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
以及嵌套属性如:
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
我想你是在 PL/SQL 中操作这些,但原理是一样的。
不知道您为什么将 emp_id 和 salary 存储为字符串。