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

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 ------

-- Om te hard groeien (en daarmee performance-problemen) te voorkomen, zorgen
-- onderstaande constraints ervoor dat oude records in fmhn_imp_org en
-- fmhn_imp_prs automatisch worden geschoond als de gerelateerde records uit
-- fac_import worden geschoond.
DELETE FROM fmhn_imp_org x WHERE NOT EXISTS (SELECT 1 from fac_import WHERE fac_import_key = x.fac_import_key);
COMMIT;

ALTER TABLE fmhn_imp_org ADD CONSTRAINT fmhn_c_fac_import_key_org FOREIGN KEY (fac_import_key) REFERENCES fac_import (fac_import_key) ON DELETE CASCADE;
COMMIT;

DELETE FROM fmhn_imp_prs x WHERE NOT EXISTS (SELECT 1 from fac_import WHERE fac_import_key = x.fac_import_key);
COMMIT;

ALTER TABLE fmhn_imp_prs ADD CONSTRAINT fmhn_c_fac_import_key_prs FOREIGN KEY (fac_import_key) REFERENCES fac_import (fac_import_key) ON DELETE CASCADE;
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
