批量收集PL / SQL

时间:2013-10-23 17:13:59

标签: oracle plsql

通过使用批量收集,如何检测何时在尝试循环结果时抛出不返回结果的查询以防止异常?

2 个答案:

答案 0 :(得分:2)

文件中的一个例子:
http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/tuning.htm#BABCCJCB

  OPEN c1;
  LOOP
    FETCH c1 BULK COLLECT INTO names, sals LIMIT v_limit;
    EXIT WHEN names.COUNT = 0;
    print_results();
  END LOOP;
  CLOSE c1;

答案 1 :(得分:0)

您可以使用此代码:

open rc for select descr from hardware;
loop
    fetch rc bulk collect into l_rows limit l_fetch_sizes(i);
    exit when rc%notfound;
end loop;
close rc;

退出时完成循环

要捕获批量收集DML操作的例外,您可以 SQL%BULK_EXCEPTIONS

BEGIN
    -- DML operations
EXCEPTION
    WHEN operation_erros THEN
    l_error := SQL%BULK_EXCEPTIONS.count;
    FOR ind IN 1 .. l_error LOOP
        DBMS_OUTPUT.put_line('Error: ' || ind || ' Array Index: ' || SQL%BULK_EXCEPTIONS(ind).ERROR_INDEX || ' Message: ' || SQLERRM(-SQL%BULK_EXCEPTIONS(ind).ERROR_CODE));
    END LOOP;
END;