--
-- $Id$
--
-- Customer specific once-script MNNL#68520.sql.
DEFINE thisfile = 'MNNL#68520.sql'
DEFINE dbuser   = '^MNNL'

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

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

-- MNNL#68520: Omkatten afgemelde Security-meldingen van foutief Behandelteam
--             naar Security-Behandelteam op basis van 'Eigen tabel'-import!

-- *** PRE-CONDITIE ***
-- Alleen inlezen (niet verwerken!) van bestand via 'FIP: Aanmaken/bijwerken
-- eigen tabel'-import!

-- Verwijder FAC_IMPORT-entry voor vandaag/zojuist door _FACILITOR ingelezen
-- bestand (om verwerken te voorkomen).
-- NB. De FAC_IMPORT_USRDATA-tabel blijft gevuld!
DELETE FROM fac_import
      WHERE fac_import_app_key = 23                     -- FIP: Eigen tabellen
            AND TRUNC (fac_import_datum_gelezen) = TRUNC (SYSDATE)
            --AND fac_import_filenaam = 'InventarisatieMNNL#68520.csv'
            AND prs_perslid_key = 4                              -- _FACILITOR
;
COMMIT;
/*
SELECT m.mld_ins_discipline_key,
       (SELECT ins_discipline_omschrijving
          FROM ins_tab_discipline
         WHERE ins_discipline_key = m.mld_ins_discipline_key),
       (SELECT md.ins_discipline_key
          FROM fac_imp_usrdata ud, mld_discipline md
         WHERE fac.safe_to_number (ud.fac_usrdata_code) = m.mld_melding_key
               AND ud.fac_usrdata_omschr = md.ins_discipline_omschrijving),
       (SELECT md.ins_discipline_omschrijving
          FROM fac_imp_usrdata ud, mld_discipline md
         WHERE fac.safe_to_number (ud.fac_usrdata_code) = m.mld_melding_key
               AND ud.fac_usrdata_omschr = md.ins_discipline_omschrijving)
  FROM mld_melding m
 WHERE EXISTS
          (SELECT 1
             FROM fac_imp_usrdata ud
            WHERE fac.safe_to_number (fac_usrdata_code) = m.mld_melding_key);
*/
UPDATE mld_melding m
   SET m.mld_ins_discipline_key =
          (SELECT md.ins_discipline_key
             FROM fac_imp_usrdata ud, mld_discipline md
            WHERE ud.fac_usrdata_omschr = md.ins_discipline_omschrijving
                  AND fac.safe_to_number (ud.fac_usrdata_code) = m.mld_melding_key)
 WHERE EXISTS
          (SELECT 1
             FROM fac_imp_usrdata ud, mld_discipline md
            WHERE ud.fac_usrdata_omschr = md.ins_discipline_omschrijving
                  AND fac.safe_to_number (ud.fac_usrdata_code) = m.mld_melding_key);
COMMIT;

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

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

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