Oracle Bulk Collect问题

gnu*_*chu 2 sql oracle plsql

我有一个关于一块批量收集sql的小问题,我希望你可以帮忙.

使用以下代码:

declare
    cursor c1
    is
    select customer,product
    from products;

    type type_cust is table of products.customer%type;
    type type_prod is table of products.product%type;

    v_array_cust    type_cust;
    v_array_prod    type_prod;
begin
    open c1;
    loop
        fetch c1 
        into v_array_cust, v_array_prod
        limit 1000;

        exit when c1%notfound;

        for i in 1..v_array_cust.count
        loop
            --Do some processing here.
        end loop;
    end loop;
end;
/
Run Code Online (Sandbox Code Playgroud)

光标c1返回53166行.

但是,代码处理53000行然后结束.似乎在去检索最后166条记录时会出现某种失败.

如果找到的记录少于1000条记录,那么fetch会返回%notfound吗?我应该将出口移动到循环的末尾吗?(我将尝试这个,但它深入一段代码需要3个小时才能达到失败点.)

提前致谢.

gnu*_*chu 9

好吧,比我已经做过的更好的谷歌搜索给了我答案你不应该使用%notfound有限制.

点击这里查看解释.


Jef*_*emp 5

仅供参考,这里有一个使代码正确运行的简单更改:

open c1;
loop
    fetch c1 
    into v_array_cust, v_array_prod
    limit 1000;

    -- the count will be 0 if no rows were found, so this loop will do nothing.
    for i in 1..v_array_cust.count
    loop
        --Do some processing here.
    end loop;

    -- exit now if the last fetch got the last set of rows
    exit when c1%notfound;
end loop;
close c1;
Run Code Online (Sandbox Code Playgroud)