Oracle
 sql >> Database >  >> RDS >> Oracle

Impossibile compilare PL/SQL con BULK COLLECT e FORALL

Non puoi* fare riferimento a singoli campi quando utilizzi FORALL - ecco perché viene visualizzato l'errore PLS-00436.

Per aggirare questo problema, dovrai utilizzare array associativi per fare riferimento a singoli campi.

DECLARE

    TYPE tt_rectype IS RECORD (
      referencekey tt.referencekey%TYPE,
      spid tt.spid%TYPE,
      nnsp tt.hiredate%TYPE,
      onsp tt.deptno%TYPE,
      portingtn tt.portingtn%TYPE);

    TYPE tt_aa_type
      IS TABLE OF TT_RECTYPE INDEX BY PLS_INTEGER;

    tt_aa TT_AA_TYPE;
    CURSOR cur_data IS
      SELECT *
      FROM   tt
      WHERE  ROWID IN (SELECT ROWID
                       FROM   (SELECT ROWID,
                                      Row_number () over (PARTITION BY portingtn
                                      ,
                                      nnsp
                                      , onsp,
                                      spid,
                                      Trunc(
                                              createddate
                                      , 'MI') ORDER BY portingtn) dup
                               FROM   tt)
                       WHERE  dup > 1);
BEGIN
    OPEN cur_data;

    LOOP
        FETCH cur_data BULK COLLECT INTO tt_aa LIMIT 1000;

        FORALL i IN 1..tt_aa.COUNT
          INSERT INTO soa_temp_sv_refkey_fordelete
                      (referencekey,
                       spid,
                       nnsp,
                       onsp,
                       portingtn)
          (SELECT referencekey,
                  spid,
                  nnsp,
                  onsp,
                  portingtn
           FROM   tt
           WHERE  portingtn = Tt_aa(i).portingtn
                  AND spid = Tt_aa(i).spid
                  AND nnsp = Tt_aa(i).nnsp
                  AND onsp = Tt_aa(i).onsp
                  AND svid IS NULL);

        EXIT WHEN cur_data%notfound;
    END LOOP;

    CLOSE cur_data;

    COMMIT;
END; 

*nota che questa limitazione non è più presente in Oracle 11g+

Inoltre, come @jonearles commenti , potresti semplicemente usare una singola istruzione SQL....

INSERT INTO soa_temp_sv_refkey_fordelete
            (referencekey,
             spid,
             nnsp,
             onsp,
             portingtn)
SELECT referencekey,
       spid,
       nnsp,
       onsp,
       portingtn
FROM   tt
WHERE  ROWID IN (SELECT ROWID
                 FROM   (SELECT ROWID,
                                Row_number () over (PARTITION BY portingtn, nnsp
                                , onsp,
                                spid,
                                Trunc(
                                        createddate
                                , 'MI') ORDER BY portingtn) dup
                         FROM   tt)
                 WHERE  dup > 1);