Convert FOR statement to FORALL in PL/SQL Oracle

386 views Asked by At

Is this possible to convert this "for" in "forall" ?

FOR  tempCounter in tempCollection.FIRST .. tempCollection.LAST LOOP
  IF tempCollection(tempCounter).execactstockkey IS NULL THEN
    RETURN;
  END IF;
  INSERT INTO  tbexectempactstock VALUES  tempCollection(tempCounter);
END LOOP; 

Also I tried this

FORALL tempCounter in tempCollection.FIRST .. tempCollection.LAST
  INSERT WHEN tempCollection(i).execactstockkey IS NOT NULL
    THEN INTO tbexectempactstock VALUES tempCollection(tempCounter);

But it pops up me missing SELECT KEYBOARD

2

There are 2 answers

0
kapil On

You can use not null clause

   FORALL tempCounter  IN tempCollection.FIRST .. tempCollection.LAST
    INSERT INTO tbexectempactstock 
    SELECT
        *
    FROM TABLE(tempCollection(tempCounter))
    WHERE tempCollection(tempCounter).execactstockkey IS NOT NULL;
1
Radagast81 On

I doubt that you can transform this conditional statement into a FORALL INSERT. A FORALL MERGE might be possible, but the best way is in my opinion to do it in a single insert:

INSERT INTO tbexectempactstock
SELECT * FROM TABLE(tempCollection)
 WHERE execactstockkey IS NOT NULL