理解游标(2)游标的属性介绍及不同游标类别的案例演示(二)
t是集合中的所有记录
dbms_output.put_line(rowValue.a);--取出集合中的值进行打印
end loop;
end;
declare
begin
--可以将select语句for xx in 内,无论传参或是嵌套更为方便简洁。
for rowValue in (select level a from dual connect by level <=10) loop
for rv in (select name from t where t.id = rowValue.a) loop
dbms_output.put_line(rv.name );--将取到的值打印。
end loop;
end loop;
end;
--这种是最简单的游标
declare
begin
for rowValue in 1..10 loop
dbms_output.put_line(rowValue);--将取到的值打印,如果查询只有一个字段这里只需写变量名即可。
end loop;
end;
⑤ REF游标实例:
[sql]
declare
type cus_cur_type is ref cursor return t%rowtype; --强类型Ref游标,查询的sql必须返回t表类型
--type cus_cur_type is ref cursor;弱类型Ref游标,返回类型没有限制
rowList cus_cur_type; -- 声明游标变量
rowValue t%rowtype; --声明行变量
begin
open rowList for select * from t; --打开游标,并关联sql
loop
fetch rowList
into rowValue; --按行取出数据
exit when rowList%notfound;--判断是否还存在记录,如果不存在终止游标
dbms_output.put_line(rowValue.name);--将取到的值打印,如果查询只有一个字段这里只需写变量名即可。
end loop;
close rowList;--关闭游标
end;