Oracle 遍历游标的四种方式汇总(for、fetch、while、BULK COLLECT)

2022-07-29,,,,

1.情景展示

  oracle 遍历游标四种方式(for、fetch、while、bulk collect+forall)

2.问题分析

  我们可以把游标想象成一张表,想要遍历游标,就要取到游标的每行数据,所以问题的关键就成了:如何取到行数据?

3.解决方案

  方式一:for 循环(推荐使用)

  变形一:遍历显式游标

/* 如果是在存储过程外使用显式游标,需要使用declare关键字 */
declare
  /*创建游标*/
  cursor cur_first_index is
    select a.id        a_id, --一级指标id
           a.indexname a_indexname --一级指标名称
      from index_a a
     order by a_id;
  /*定义游标变量,该变量的类型为基于游标cur_first_index的行记录*/
  row_cur_first_index cur_first_index%rowtype;
/*游标处理*/
begin
  /*遍历显式游标*/
  --for 循环
  for row_cur_first_index in cur_first_index loop
    --循环体
    dbms_output.put_line('{"id":"' || row_cur_first_index.a_id || '","名称":"' || row_cur_first_index.a_indexname || '"}');
  end loop;
end;  

  执行,输出结果

  变形二:遍历隐式游标(推荐使用)

  for循环遍历游标,其实又可以分为两种方式,一种是显式游标的遍历,另一种是隐式游标的遍历。

/* 如果是在存储过程外使用隐式游标,如果用不到变量无需声明declare关键字 */
/*游标处理*/
begin
  /*遍历隐式游标*/
  --for 循环
  for row_cur_first_index in (select a.id        a_id, --一级指标id
                                     a.indexname a_indexname --一级指标名称
                                from index_a a
                               order by a_id) loop
    --循环体
    dbms_output.put_line('{"id":"' || row_cur_first_index.a_id || '","名称":"' || row_cur_first_index.a_indexname || '"}');
  end loop;
end;

  隐式游标相较于显式游标用法更加简单,无需声明直接调用即可。    

  方式二:fetch 循环

/*游标声明代码和方式一一致,此处省略,直接展示游标处理代码*/
begin
  /*遍历游标*/
  --fetch 循环
  open cur_first_index; --必须要明确的打开和关闭游标
  loop
    fetch cur_first_index into row_cur_first_index;
    exit when cur_first_index%notfound;
    --循环体
    dbms_output.put_line('{"id":"' || row_cur_first_index.a_id || '","名称":"' || row_cur_first_index.a_indexname || '"}');
  end loop;
  close cur_first_index;
end;

  方式三:while 循环

/*游标声明代码和方式一一致,此处省略,直接展示游标处理代码*/
begin
  /*遍历游标*/
  open cur_first_index; --必须要明确的打开和关闭游标
  fetch cur_first_index
    into row_cur_first_index;
  while cur_first_index%found loop
    --循环体
    dbms_output.put_line('{"id":"' || row_cur_first_index.a_id || '","名称":"' || row_cur_first_index.a_indexname || '"}');
    fetch cur_first_index
      into row_cur_first_index;
  end loop;
  close cur_first_index;
end;  

  注意:使用while循环时,需要fetch两次。

  方式四:bulk collect+forall(速度最快)

/* 如果是在存储过程外使用显示游标,需要使用declare关键字 */
/*声明游标*/
declare
  /*创建显式游标*/
  cursor cur_first_index is
    select a.id        a_id, --一级指标id
           a.indexname a_indexname --一级指标名称
      from index_a a
     order by a_id;
  /*定义表类型,该表的表结构为游标cur_first_index的行记录(可以存储多条游标记录)*/
  type table_cur_first_index is table of cur_first_index%rowtype;
  /* 声明表变量*/
  tab_first_index table_cur_first_index;
/*游标处理过程*/
begin
  /*遍历游标*/
  open cur_first_index;
  loop
    --将n行游标数据放到表中
    fetch cur_first_index bulk collect
      into tab_first_index limit 1; -- 数据量太少,仅当前测试使用哦,实际开发建议 500 左右
    -- 退出条件
    exit when tab_first_index.count = 0;
    --循环表数据
    forall i in tab_first_index.first .. tab_first_index.last loop
      dbms_output.put_line('{"id":"' || tab_first_index(i).a_id || '","名称":"' || tab_first_index(i).a_indexname || '"}');
    end loop;
  end loop;
  close cur_first_index;
end;

4.总结

  •   使用for循环的优势在于:

  不需要手动打开&关闭游标(声明游标的开启和关闭);

  不需要手动捕获数据(自动将数据fetch到记录型变量);

  不需要关注何时要退出,也就是不需要写退出循环的满足条件(遍历完成就会退出)。

  •   第4方式与前3种的区别在于:

  前三种的游标变量:row_cur_first_index,只能存储游标的一条数据;

  第四种的表变量:tab_first_index,可以存储游标的多条数据。

  大数据批量处理的时候,第4种方式的优势将会凸显出来。

本文作者:marydon

原文链接:https://www.cnblogs.com/marydon20170307/p/12869692.html

以上就是oracle 遍历游标的四种方式汇总(for、fetch、while、bulk collect)的详细内容,更多关于oracle 遍历游标的资料请关注其它相关文章!

《Oracle 遍历游标的四种方式汇总(for、fetch、while、BULK COLLECT).doc》

下载本文的Word格式文档,以方便收藏与打印。