loop
fetch cl into var1,var2,; -------提取数据到变量中
if cl%found then -----------如果提取到数据,则进行相应操作
......;
else ---------如果没有提取到数据,则退出循环
exit;
end if;
end loop;
declare
cursor cursor_temp IS select name,age,address,id from cip_temps where id=10;
v_name cip_temps.name%TYPE;
v_age cip_temps.age%TYPE;
v_address cip_temps.address%TYPE;
v_id cip_temps.id%TYPE;
begin
open cursor_temp;
loop
fetch cursor_temp into v_name,v_age,v_address,v_id;
exit when cursor_temp%notfound;
dbms_output.put_line(v_name ||':'|| v_age ||':'|| v_address||':'|| v_id);
end loop;
close cursor_temp;
end;
(2)、在显示游标中,使用fetch..bulk collect into 语句提取所有数据
从oracle9i开始,通过使用fetch..bulk collect into 语句,一次就可以提取结果集的所有数据,示例如下:
declare
cursor temp_cursor is select * from cip_temps where id=1;
type temp_table_type is table of cip_temps%rowtype;
temp_table temp_table_type;
begin
open temp_cursor;
fetch temp_cursor bulk collect into temp_table;
for i in 1..temp_table.count loop
dbms_output.put_line(temp_table(i).name||':'||temp_table(i).age||':'||temp_table(i).address||':'||temp_table(i).id);
end loop;
close temp_cursor;
end;
declare
type temp_array_type is array(5) of cip_temps%rowtype;
temp_array temp_array_type;
cursor temp_cursor is select * from cip_temps;
rows int:=5;
v_count int:=0;
begin
open temp_cursor;
loop
fetch temp_cursor bulk collect into temp_array limit rows;
dbms_output.put_line('message:');
for i in 1..(temp_cursor%rowcount-v_count) loop
dbms_output.put_line(temp_array(i).name||':'||temp_array(i).age||':'||temp_array(i).address||':'||temp_array(i).id);
end loop;
v_count:=temp_cursor%rowcount;
exit when temp_cursor%notfound;
end loop;
close temp_cursor;
end;
declare
type name_table_type is table of cip_temps.name%type index by binary_integer;
name_table name_table_type;
cursor temp_cursor is select name from cip_temps;
begin
if not temp_cursor%isopen then
open temp_cursor;
end if;
fetch temp_cursor bulk collect into name_table;
dbms_output.put_line(temp_cursor%rowcount);
close temp_cursor;
end;