我在包规范中定义了记录类型,表类型和函数。
TYPE name_RECORD IS RECORD (
name VARCHAR2(244),
surname VARCHAR2(244) );
TYPE name_TABLE IS TABLE OF name_RECORD;
function f_deps
(i_id_dept IN employees.id_department%type)
return name_TABLE;
并编写了在包体内返回表类型的函数。
function f_deps
(i_id_dept IN employees.id_department%type)
return name_TABLE is
CURSOR c1 IS (select * from employees );
t_name name_TABLE;
rec_name name_RECORD;
BEGIN
t_name := name_TABLE();
for i in c1
LOOP
select name, surname BULK COLLECT INTO t_name from employees where id_department = i_id_dept ;
END LOOP;
return t_name;
END f_deps;
函数编译正常,但是当我尝试执行这样的函数时:
select * from table( PACKAGE_employees.f_deps ('6')) ;
我收到此错误:
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Error at Line: 29 Column: 22
更新:我在命令行中使用CREATE TYPE语句定义了类型,就像Bob Jarvis建议的那样,但我仍然得到相同的错误消息
create or replace type name_RECORD as object (
name VARCHAR2(244),
surname VARCHAR2(244) );
create or replace type name_TABLE AS TABLE OF name_RECORD;
答案 0 :(得分:1)
您似乎无法直接在SQL中选择使用此功能,如下所示:
select * from table( PACKAGE_employees.f_deps ('6')) ;
但你可以这样做:
declare
coll package_employees.name_table;
begin
coll := package_employees.f_deps ('6');
for r in (select * from table(coll)) loop
dbms_output.put_line(r.name);
end loop;
end;
你的功能代码不对,它应该更像是这样:
function f_deps
(i_id_dept IN integer)
return name_TABLE is
t_name name_TABLE;
rec_name name_RECORD;
BEGIN
select name_record(name, surname)
BULK COLLECT INTO t_name
from employees where id_department = i_id_dept ;
return t_name;
END f_deps;
即。
name_record
类型的值,以批量收集到name_record
的集合中。