Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk Collect Twice over same nested table

Is there any way that after the second bulk collect, data does not get override of the first bulk collect. I don't want to iterate in loop.

    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;

OUTPUT:

  • p_id is E3
  • p_id is E4

Note: E1 and E2 is present in part table.

like image 494
Gaurav Soni Avatar asked Nov 14 '12 15:11

Gaurav Soni


1 Answers

You can't simply add the data to the collection, no.

You can, however, do a BULK COLLECT into a separate collection and then combine the collections assuming that you really just need/ want a nested table rather than an associative array...

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;

If you really want to use an associative array, you would need to write some code because there is no way for Oracle to know automatically how to remap the associations of one array when you combine it with another associative array that has some of the same keys.

like image 149
Justin Cave Avatar answered Sep 29 '22 23:09

Justin Cave