日期:2014-05-16 浏览次数:21319 次
declare --声明需要集合类型及变量,参照字段的 type 来声明类型 type id_type is table of sr_contacts.sr_contact_id%type; v_id id_type; type phone_type is table of sr_contacts.contact_phone%type; v_phone phone_type; type remark_type is table of sr_contacts.remark%type; v_remark remark_type; cursor all_contacts_cur is --用 rownum 来限定取出的记录数来测试 select sr_contact_id,contact_phone,remark from sr_contacts where rownum <= 50 begin open all_contacts_cur; loop fetch all_contacts_cur bulk collect into v_id,v_phone,v_remark limit 20 exit when all_contacts_cur%notfound; for i in 1..v_id.count loop --遍历集合 --用 v_id(i)/v_phone(i)/v_remark(i) 取出字段值来执行你的业务逻辑 null; --这里只放置一个空操作,只为测试循环取数的效率 dbms_output.put_line('here'); end loop; end loop; close all_contacts_cur; end;
fetch all_contacts_cur bulk collect into v_id,v_phone,v_remark limit 20; for i in 1..v_id.count loop --遍历集合 --用 v_id(i)/v_phone(i)/v_remark(i) 取出字段值来执行你的业务逻辑 null; --这里只放置一个空操作,只为测试循环取数的效率 dbms_output.put_line('here'); end loop; [color=#FF0000] exit when all_contacts_cur%notfound;[/color]
------解决方案--------------------
什么版本的数据库? 10.2.0.1的可以. 下面是执行的LOG.
SQL> SET SERVEROUTPUT ON
SQL> DECLARE
2 CURSOR CUR_1 IS
3 SELECT TABLE_NAME FROM DBA_TABLES
4 WHERE ROWNUM < 50;
5 TYPE T_CUR_1 IS TABLE OF CUR_1%ROWTYPE;
6 V_CUR1 T_CUR_1;
7
8 BEGIN
9 OPEN CUR_1;
10 LOOP
11 FETCH CUR_1 BULK COLLECT INTO V_CUR1 LIMIT 20;
12 FOR X IN 1..V_CUR1.COUNT LOOP
13 NULL;
14 DBMS_OUTPUT.PUT_LINE(X);
15 END LOOP;
16 EXIT WHEN CUR_1%NOTFOUND;
17 END LOOP;
18 CLOSE CUR_1;
19
20 END;
21 /
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16