日期:2014-05-17  浏览次数:20851 次

不能将多行数据一次性读入一个记录表吗
declare
type   res_table_type   is   table   of   table_name%rowtype   index   by   binary_integer;
res_table   res_table_type;

begin
select   *   into   res_table   from   table_name;

end;

这样好像不行,有人能帮忙吗?

------解决方案--------------------
因为不是一个table_name,而是一个cursor.

给你一个例子:

declare
cursor emp_cursor is select ename,sal from emp
where lower(job) = lower( '&job ');
type emp_table_type is table of emp_cursor%rowtype
index by binary_integer;
emp_table emp_table_type;
i int;
BEGIN
open emp_cursor;
loop
i:=emp_cursor%rowcount+1;
fetch emp_cursor into emp_table(i);
exit when emp_cursor%notfound;
dbms_output.put_line( '姓名: '||emp_table(i).ename|| ',工资: '||emp_table(i).sal);
end loop;
END;

------解决方案--------------------
楼上的不理解记录表,而且游标打开记录集也不是这样的打开法。
应该为
select * bulk collect into res_table from table_name;
对,就是应该加上bulk collect into ,当把一个集合赋值给一个集合变量(可能是索引表或记录表什么的)要用bulk collect
如果要用游标的形式,这样
declare
mycurs refcursor;
begin
open mycurs for select * from tablename;
....
end;
不过数据量大时尽量不要使用游标.