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

DEFINE thisfile = 'CWTD#38793.SQL'
DEFINE dbuser   = '^CWTD'

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

-- Dit script uitvoeren als er voor het eerst een personenimport gedaan wordt.
update prs_perslid
set prs_perslid_nr = (select prs_perslid_nr from cwtd_imp_persoon
where substr(prs_perslid.prs_perslid_email, 1, instr(prs_perslid.prs_perslid_email, '@')-1) = 
      substr(cwtd_imp_persoon.prs_perslid_email, 1, instr(cwtd_imp_persoon.prs_perslid_email, '@')-1));

update prs_perslid p
set prs_perslid_verwijder = sysdate
WHERE   NOT EXISTS
        (
        SELECT NULL
        FROM    cwtd_imp_persoon c
        WHERE   substr(p.prs_perslid_email, 1, instr(p.prs_perslid_email, '@')-1) = 
                substr(c.prs_perslid_email, 1, instr(c.prs_perslid_email, '@')-1)
        ) 
        AND NOT EXISTS
        (SELECT NULL
        FROM prs_v_verplichting_keys pv
        WHERE pv.prs_perslid_key = p.prs_perslid_key
        )
        AND p.prs_perslid_key > 50
        AND p.prs_srtperslid_key > 1;

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