--
-- $Id$
--
-- Script containing customer specific sql statements for the ARAI database
-- Omhangen van kenmerk EBMS en RAI medewerker van vakgroeptype naar vakgroep niveau.
-- ALLEEN UITVOEREN ALS DE TRANSPORT FLEXKENMERKEN IS GEDAAN.

DEFINE thisfile = 'ARAI#54456.SQL'
DEFINE dbuser   = '^ARAI'

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

BEGIN
FAC.INITSESSION('NL');
END;
/

DECLARE
   CURSOR n_kenmerkebms
   IS
SELECT m.mld_melding_key,
       m.mld_stdmelding_key,
       k.mld_kenmerk_key,
       km.mld_kenmerkmelding_key,
       km.mld_kenmerkmelding_waarde,
       sub.mld_kenmerk_key nieuw
  FROM mld_melding m,
       mld_kenmerk k,
       mld_kenmerkmelding km,
       (SELECT m.mld_melding_key, k.mld_kenmerk_key, std.mld_stdmelding_key
          FROM mld_kenmerk k,
               mld_discipline md,
               mld_stdmelding std,
               mld_melding m
         WHERE     mld_srtkenmerk_key = 141               -- Kenmerk EBMS
               AND k.mld_stdmelding_key = md.ins_discipline_key
               AND std.mld_ins_discipline_key = md.ins_discipline_key
               AND m.mld_stdmelding_key = std.mld_stdmelding_key) sub
 WHERE     k.mld_kenmerk_key = km.mld_kenmerk_key
       AND km.mld_melding_key = m.mld_melding_key
       AND k.mld_srtkenmerk_key = 141              -- Kenmerk EBMS
       AND k.mld_kenmerk_verwijder IS NULL
       AND m.mld_melding_key = sub.mld_melding_key;

 BEGIN
   FOR rec IN n_kenmerkebms
   LOOP
      BEGIN
         UPDATE mld_kenmerkmelding
            SET mld_kenmerk_key = rec.nieuw
          WHERE mld_melding_key = rec.mld_melding_key
                AND mld_kenmerk_key = rec.mld_kenmerk_key
                AND mld_kenmerkmelding_key = rec.mld_kenmerkmelding_key
                AND mld_kenmerkmelding_verwijder IS NULL;
      END;
   END LOOP;
   COMMIT;
END;
/
       
DECLARE
   CURSOR n_kenmerkmdw
   IS
SELECT m.mld_melding_key,
       m.mld_stdmelding_key,
       k.mld_kenmerk_key,
       km.mld_kenmerkmelding_key,
       km.mld_kenmerkmelding_waarde,
       sub.mld_kenmerk_key nieuw
  FROM mld_melding m,
       mld_kenmerk k,
       mld_kenmerkmelding km,
       (SELECT m.mld_melding_key, k.mld_kenmerk_key, std.mld_stdmelding_key
          FROM mld_kenmerk k,
               mld_discipline md,
               mld_stdmelding std,
               mld_melding m
         WHERE     mld_srtkenmerk_key = 61  -- RAI mdw
               AND k.mld_stdmelding_key = md.ins_discipline_key
               AND std.mld_ins_discipline_key = md.ins_discipline_key
               AND m.mld_stdmelding_key = std.mld_stdmelding_key) sub
 WHERE     k.mld_kenmerk_key = km.mld_kenmerk_key
       AND km.mld_melding_key = m.mld_melding_key
       AND k.mld_srtkenmerk_key = 61        -- RAI mdw
       AND k.mld_kenmerk_verwijder IS NULL
       AND m.mld_melding_key = sub.mld_melding_key;      

BEGIN
   FOR rec IN n_kenmerkmdw
   LOOP
      BEGIN
         UPDATE mld_kenmerkmelding
            SET mld_kenmerk_key = rec.nieuw
          WHERE mld_melding_key = rec.mld_melding_key
                AND mld_kenmerk_key = rec.mld_kenmerk_key
                AND mld_kenmerkmelding_key = rec.mld_kenmerkmelding_key
                AND mld_kenmerkmelding_verwijder IS NULL;
      END;
   END LOOP;
   COMMIT;
END;
/
                                            
------ 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