FORALL SELECT 找不到数据

FORALL SELECT getting NO DATA FOUND

类似于发布的问题 (), 我最近才发现我可以将 FORALLSELECT 一起使用。所以我在 PL/SQL 块中尝试了以下代码:

FORALL i in p_eit_hdr_rec.first..p_eit_hdr_rec.last
select  count(*)
into    l_exists
from    TABLE_X x1
    ,   TABLE_Y x2
    ,   TABLE_Z x3
where   x2.batch_name = p_eit_hdr_rec(i).batch_name
and     x1.person_id  = x2.person_id
and     x2.status     = 'New'
and     x3.information_type = x1.information_type
and     x3.EIT_INT_HDR_ID   = x2.EIT_INT_HDR_ID;

其中 p_eit_hdr_rec 是关联数组,p_eit_hdr_rec(i).batch_name 包含字符串 'hire_20170711_200005.csv',l_exists 是默认为 0 的 NUMBER 变量。 我得到 NO_DATA_FOUND 异常,即使我在正常查询时确认有数据正在获取 SQL:

select  count(*)
into    l_exists
from    TABLE_X x1
    ,   TABLE_Y x2
    ,   TABLE_Z x3
where   x2.batch_name = 'hire_20170711_200005.csv'
and     x1.person_id  = x2.person_id
and     x2.status     = 'New'
and     x3.information_type = x1.information_type
and     x3.EIT_INT_HDR_ID   = x2.EIT_INT_HDR_ID;

COUNT(*)
--------
2

为什么我得到异常? FORALL SELECT 是一个有效的语法但对检查数据无效吗?

FOR ALL 语句通常用于执行像 INSERT UPDATE 这样的 DML 语句 DELETE.That 是你得到异常的原因。

除了其他评论之外,这里还有一个如何将 FORALL 与 BULK COLLECT 结合使用的示例:

declare 
  TYPE ARRAY IS TABLE OF TABLE1%ROWTYPE;
  obj_data ARRAY;
  i integer;
  CURSOR c1 IS 
    SELECT * FROM TABLE1;
begin
  OPEN c1;
    LOOP
      FETCH c1 BULK COLLECT INTO obj_data limit 32768;

      FORALL i IN 1..obj_data.COUNT
        INSERT /*+ append */ INTO TABLE2 
          VALUES obj_data(i);

      EXIT WHEN c1%NOTFOUND;      
    END LOOP;
    commit;
  CLOSE c1;
end;
/

I recently just found out that i can use FORALL with SELECT.

不,你不能。

读精manual about FORALL Statement:

dml_statement

A static or dynamic INSERT, UPDATE, DELETE, or MERGE statement that references at least one collection in its VALUES or WHERE clause. Performance benefits apply only to collection references that use index as an index.

因此,不能将 forallselect 一起使用。对应的bulk SQL operation for select is SELECT INTO Statement with BULK COLLECT Clause.

祝您阅读愉快!

一个非常简单的解释是您的关联数组可能包含元素列表而不是单个元素。现在您已尝试仅使用一个元素来查询 SQL 语句。

其次FORALL SELECT如上所述是不可能的。您需要遍历每条记录,然后找到计数。这将是我建议的替代方案。