Sql Oracle批量收集问题

Sql Oracle批量收集问题,sql,oracle,plsql,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 typ

我有一个关于批量收集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;
/
光标c1返回53166行

但是,代码处理53000行,然后结束。似乎在检索最后166条记录时出现了某种故障

如果找到的记录少于1000条,提取是否会返回%notfound?我应该把出口移到环路的末端吗?(我将尝试这一点,但它深入到一段代码中,需要3个小时才能到达故障点。)


提前谢谢。

好的,比我之前做的更好的谷歌搜索给了我一个答案,你不应该使用%notfound with limit


请查看解释。

很抱歉要这么说,但请确认!光标!。。。从你写这篇文章的方式来看,你有线性规划的背景。您考虑过基于集合的解决方案吗?

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

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;

这有点自以为是,因为他没有指定“在这里做一些处理”位的作用。这可能是在SQL中不可能或效率低下的事情。另外,他一次将1000行提取到内存中,这通常是在PL/SQL中处理大型数据集的一种非常有效的方法我只是想确定“我真的需要一个循环/游标吗?”这个问题被问到了。我确实有线性编程的背景。但这不是我的代码——我支持的是遗留代码。还有别的选择吗?我不明白你所说的“基于集合的解决方案”是什么意思。我必须看看“一些处理包括”是什么,但实际上简化了,线性是一次一条记录,而setbased是同时做所有记录(整个“集合”)。我猜这里的原始编码员尝试了所有50k记录的循环,但后来不得不将其分成1000个记录段……但这纯粹是猜测。在关系数据模型中,很少有事情是不能基于集合完成的,因此,如果不能将其重写为基于集合的逻辑,我会感到惊讶……可能会在整个查询过程中看到相当好的速度改进。我选择了“当v_array_cust.count=0时退出”,这同样有效。是的,这同样有效-没有多大区别,但您的方法将涉及从光标进行额外的不必要的提取(但这只是我的挑剔)@JeffreyKemp您认为这不是材料吗?让我们假设这个循环在另一个循环中。如果你在一个紧密的循环中多次运行这个循环,我会寻找另一个解决方案。使用“count=0”或“exit when%notfound”之间的区别可能无法检测到,因为大多数时间将用于运行查询和在内部循环中进行逐行处理。顺便说一句,我不同意这篇文章。说“不要使用%notfoundwithlimit”是错误的。使用%notfound with limit没有什么错,只要您将它们按正确的顺序排列(请参阅我的答案)。