oracle从游标批量提取数据

来源于:http://blog.csdn.net/ceclar123/article/details/7974973

 

传统的fetch into一次只能取得一条数据,使用fetch bulk collect into可以一次从游标中取得所有数据,使用limit子句可以限制一次取的数据条数

1、fetch bulk collect into


 
  1. begin  
  2.   declare     
  3.     cursor c_dept is select * from dept;  
  4.     type dept_record is table of dept%rowtype;  
  5.     v_dept dept_record;  
  6.   begin  
  7.     open c_dept;  
  8.     fetch c_dept bulk collect into v_dept;  
  9.     close c_dept;  
  10.     for i in 1.. v_dept.count loop  
  11.         dbms_output.put_line('部门名称:'||v_dept(i).dname||'   部门地址:'||v_dept(i).loc);       
  12.     end loop;  
  13.   end;  
  14. end;  


2、使用limit子句限制提取的行数


 
    1. begin  
    2.   declare     
    3.     cursor c_dept is select * from dept;  
    4.     type dept_record is table of dept%rowtype;  
    5.     v_dept dept_record;  
    6.   begin  
    7.     open c_dept;  
    8.     loop  
    9.       exit when c_dept%NOTFOUND;  
    10.       fetch c_dept bulk collect into v_dept limit 3;    
    11.       dbms_output.put_line('-----------------------------------------------------------');     
    12.         for i in 1.. v_dept.count loop  
    13.             dbms_output.put_line('部门名称:'||v_dept(i).dname||'   部门地址:'||v_dept(i).loc);       
    14.         end loop;  
    15.     end loop;  
    16.     close c_dept;  
    17.   end;  
    18. end;  
posted @ 2016-10-28 14:25  一个勤奋的胖子  阅读(4777)  评论(1编辑  收藏  举报