5

有没有办法在第二次批量收集之后,数据不会覆盖第一次批量收集。我不想循环迭代。

    DECLARE
       TYPE abc IS RECORD (p_id part.p_id%TYPE);

       TYPE abc_nt
       IS
          TABLE OF abc
             INDEX BY BINARY_INTEGER;

       v_abc_nt      abc_nt;
    BEGIN
       SELECT   p_id
         BULK   COLLECT
         INTO   v_abc_nt
         FROM   part
        WHERE   p_id IN ('E1', 'E2');

       SELECT   p_id
         BULK   COLLECT
         INTO   v_abc_nt
         FROM   part
        WHERE   p_id IN ('E3', 'E4');

       FOR i IN v_abc_nt.FIRST .. v_abc_nt.LAST
       LOOP
          DBMS_OUTPUT.put_line (
             'p_id is ' || v_abc_nt (i).p_id
          );
       END LOOP;
    END;

输出:

  • p_id 是 E3
  • p_id 是 E4

注意:E1 和 E2 存在于零件表中。

4

2 回答 2

11

您不能简单地将数据添加到集合中,不。

但是,您可以将 aBULK COLLECT放入一个单独的集合中,然后假设您确实只需要/想要一个嵌套表而不是关联数组来组合这些集合......

DECLARE
   TYPE abc IS RECORD (p_id part.p_id%TYPE);

   TYPE abc_nt
   IS
      TABLE OF abc;

   v_abc_nt       abc_nt;
   v_abc_nt2      abc_nt;
BEGIN
   SELECT   p_id
     BULK   COLLECT
     INTO   v_abc_nt
     FROM   part
    WHERE   p_id IN ('E1', 'E2');

   SELECT   p_id
     BULK   COLLECT
     INTO   v_abc_nt2
     FROM   part
    WHERE   p_id IN ('E3', 'E4');

   v_abc_nt := v_abc_nt MULTISET UNION v_abc_nt2;

   FOR i IN v_abc_nt.FIRST .. v_abc_nt.LAST
   LOOP
      DBMS_OUTPUT.put_line (
         'p_id is ' || v_abc_nt (i).p_id
      );
   END LOOP;
END;

如果您真的想使用关联数组,则需要编写一些代码,因为当您将一个数组与另一个具有某些相同键的关联数组组合时,Oracle 无法自动知道如何重新映射该数组的关联.

于 2012-11-14T16:05:06.630 回答
0

你可以这样写

不好的例子:

declare
  type t_numb is record(
    numb number);
  type t_numb_list is table of t_numb;
  v_numb_list t_numb_list;
begin
  with q as
   (select 1 a from dual union select 2 from dual union select 3 from dual)
  select q.a bulk collect into v_numb_list from q;
  with w as
   (select 4 a from dual union select 5 from dual union select 6 from dual)
  select w.a bulk collect into v_numb_list from w;

  for r in 1 .. v_numb_list.count loop
    dbms_output.put_line(v_numb_list(r).numb);
  end loop;
end;

这很好用:

declare
  type t_numb is record(
    numb number);
  type t_numb_list is table of t_numb;
  v_numb_list t_numb_list := t_numb_list();
  v_numb      t_numb;
begin
  for q in (select 1 a
              from dual
            union
            select 2
              from dual
            union
            select 3
              from dual) loop
    v_numb.numb := q.a;
    v_numb_list.extend;
    v_numb_list(v_numb_list.count) := v_numb;
  end loop;

  for w in (select 4 a
              from dual
            union
            select 5
              from dual
            union
            select 6
              from dual) loop
    v_numb.numb := w.a;
    v_numb_list.extend;
    v_numb_list(v_numb_list.count) := v_numb;
  end loop;

  for r in 1 .. v_numb_list.count loop
    dbms_output.put_line(v_numb_list(r).numb);
  end loop;
end;

于 2017-11-09T08:05:04.627 回答