--
-- $Id$
--
-- Script containing customer specific sql statements for the FACILITOR database

DEFINE thisfile = 'ASLE#44887.SQL'
DEFINE dbuser   = '^ASLE'
DEFINE custid   = 'ASLE'

SET ECHO ON
SET DEFINE ON
COLUMN fcltlogfile NEW_VALUE fcltlogfile NOPRINT;
WHENEVER SQLERROR EXIT;
SELECT adm.scriptspoolfile('&dbuser', '&thisfile') AS fcltlogfile FROM DUAL;
WHENEVER SQLERROR CONTINUE;
SPOOL &fcltlogfile
SET DEFINE OFF

------ payload begin ------

DELETE FROM fac_tracking
      WHERE fac_tracking_refkey IN (SELECT mld_opdr_key
                                    FROM (SELECT m.mld_melding_key, o.mld_opdr_key
                                          FROM mld_melding m, mld_opdr o
                                          WHERE m.mld_melding_einddatum < TO_DATE ('01-10-2017', 'dd-mm-yyyy')
                                          AND m.mld_melding_key = o.mld_melding_key))
        AND fac_srtnotificatie_key IN
                     (SELECT fac_srtnotificatie_key
                      FROM fac_srtnotificatie
                      WHERE fac_srtnotificatie_xmlnode IN ('opdracht'));

DELETE FROM mld_opdr
      WHERE mld_melding_key IN
                  (SELECT mld_melding_key
                     FROM mld_melding m
                    WHERE mld_melding_einddatum <
                             TO_DATE ('01-10-2017', 'dd-mm-yyyy'));

DELETE FROM fac_tracking
      WHERE fac_tracking_refkey IN
                  (SELECT mld_melding_key
                     FROM mld_melding
                    WHERE mld_melding_einddatum <
                             TO_DATE ('01-10-2017', 'dd-mm-yyyy'))
            AND fac_srtnotificatie_key IN
                     (SELECT fac_srtnotificatie_key
                        FROM fac_srtnotificatie
                       WHERE fac_srtnotificatie_xmlnode IN ('melding'));

DELETE FROM mld_melding
      WHERE mld_melding_einddatum < TO_DATE ('01-10-2017', 'dd-mm-yyyy');

------ payload end   ------

SET DEFINE OFF
BEGIN adm.systrackscriptId ('$Id$', 0); END;
/

COMMIT;
SET ECHO OFF
SPOOL OFF
SET DEFINE ON
PROMPT Logfile of this upgrade is: &fcltlogfile
