Oracle数据库存储过程--使用游标(1)
关于选用何种游标?
显示游标分为:普通游标,参数化游标和游标变量三种。
游标循环策略 :分三种循环:loop,while,for
例子:
create or replace procedure proccycle(p varchar2)
as
cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6;
v_postype varchar2(20);
v_description varchar2(50);
begin
open c_postype; if c_postype%found then
dbms_output.put_line('found true');
elsif c_postype%found = false then
dbms_output.put_line('found false');
else
dbms_output.put_line('found null');
end if;
loop
fetch c_postype into v_postype,v_description ;
exit when c_postype%notfound;
dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
end loop;
close c_postype;
dbms_output.put_line('---loop end---');
open c_postype;
fetch c_postype into v_postype,v_description;
while c_postype%found loop
dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
fetch c_postype into v_postype,v_description ;
end loop;
close c_postype;
dbms_output.put_line('---while end---');
for v_pos in c_postype loop
v_postype := v_pos.pos_type;
v_description := v_pos.description;
dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
end loop;
dbms_output.put_line('---for end---');
end;
备注:
1、使用游标之前需要开打游标,open cursor,循环完后再关闭游标close cursor.
2、在打开一个游标之后,马上检查它的%found或%notfound属性,它得到的结果即不是true也不是false.而是null.必须执行一条fetch语句后,这些属性才有值。