想创建一个返回表类型的函数,脚本如下,可总是编译不过,请大家帮我看看,谢谢!

/* create a table test2 */
create table test2(
ID1 int,
Name1 varchar2(20)
);
insert into test2 values(1,'Beijing');
insert into test2 values(2,'Tianjin');
insert into test2 values(3,'Shanghai');
/*create a table object */
create or replace type obj_table as object
(
id1 int,
name1 varchar2(20)
);
/
create or replace type t_table as table of obj_table;
/* create function returned table-object */
create or replace function f_normal(s int)
return t_table
as
rs t_table := t_table();
begin
insert into rs select * from test2;
return rs;
end f_normal;
/
------解决思路----------------------
首先,insert into后面只能跟表,不能跟别的对象
其次,select * from test2返回的是一组记录,而rs是一个数组,每个元素为obj_table类型。不能直接select into 给它
create or replace function f_normal(s int)
return t_table
as
type t_table1 is table of test2%rowtype;
rs t_table1;
begin
select * bulk collect into rs from test2;
return null;
end f_normal;
/
如果要将test2里的值赋给数组,就得用游标一一赋值
create or replace function f_normal(s int)
return t_table
as
rs t_table:=t_table();
i integer:=0;
begin
for x in(select * from test2)loop
i:=i+1;
rs.extend;
rs(i):=obj_table(x.id1,x.name1);
end loop;
return rs;
end f_normal;
/
此时函数创建成功,可以用table()函数直接查看它的值
select * from table(f_normal(null));