1.先介绍关于table变量的运用,它就相当于高级语言中的数组一样.
最简单的用法(利用bulk collect一次性读取多行):
type my_table_type is table of varchar2(20); my_table my_table_type; cursor my_cursor is select dname from dept; begin open my_cursor; fetch my_cursor bulk collect into my_table; --遍历table for i in 1..my_table.count loop dbms_output.put_line(my_table(i)); end loop; end;
当你运用table的时候可以首先给它初始化如下
my_table(1,2,3,4)代表四行一列,每一行的值一次为1,2,3,4
declare
type my_table is table of number; tabl1 my_table:=my_table(1,2,3,4);--初始化 begin dbms_output.put_line(tabl1(2)); end;
或者
my_table(var1,var2,var3)代表有三行dept%rowcount列,行值一次为var1,var2,var3
列值一次为var1.deptno,var1.deptno.dname等等。
declare
type my_table is table of dept%rowtype; var1 dept%rowtype; var2 dept%rowtype; var3 dept%rowtype; table2 my_table; begin var1.deptno:=10; table2:=my_table(var1,var2,var3); end;
create or replace type emps_table_type is object(id number, name varchar2(20))
create or replace type emps_table is table of emps_table_type; create or replace procedure myprocedure(i out emps_table) is table1 emps_table_type; --table2 emps_table; cursor mycursor is select id,name from emps; ids number; names varchar2(20); con number:=1; begin i:=emps_table(); open mycursor; fetch mycursor into ids,names; table1:=emps_table_type(ids,names); i.Extend; i(con):=table1; while mycursor%found loop con:=con+1; fetch mycursor into ids,names; table1:=emps_table_type(ids,names); i.Extend; i(con):=table1; end loop; end; declare i emps_table; begin myprocedure(i); dbms_output.put_line(i(1).name); end;
你不晓得