--  aads#28218
--
--
SET ECHO ON
SPOOL aads#28218.lst
PROMPT $Revision$
ALTER SESSION SET nls_territory='america';
SET DEFINE OFF

DECLARE
   CURSOR c
   IS
      SELECT mld_opdr_key
        FROM mld_opdr o
       WHERE mld_opdr_opmerking LIKE '%Afgesloten / Geannuleerd%'
         AND mld_statusopdr_key = 1
         AND EXISTS (SELECT fin_factuur_key
                       FROM fin_factuur f
                      WHERE f.mld_opdr_key = o.mld_opdr_key);
BEGIN
   FOR rec IN c
   LOOP
      BEGIN
         UPDATE mld_opdr
            SET mld_statusopdr_key = 6
          WHERE mld_opdr_key = rec.mld_opdr_key AND mld_statusopdr_key = 1;

         INSERT INTO fac_tracking (fac_tracking_refkey,
                                   fac_srtnotificatie_key,
                                   fac_tracking_oms)
             VALUES (rec.mld_opdr_key,
                     27,
                     'Opdracht op verzoek van AAFM afgemeld');
      END;
   END LOOP;

   COMMIT;
END;
/

BEGIN adm.systrackscript('$Workfile: aads#28218 $', '$Revision$', 1); END;
/

SPOOL OFF
