create or replace TRIGGER OU_REF.COUNTRY_H
BEFORE INSERT OR UPDATE OR DELETE ON OU_REF.COUNTRY
REFERENCING NEW AS new OLD AS old
FOR EACH ROW
DECLARE
  v_current_date DATE := SYSDATE;
  v_future_date DATE  := TO_DATE('31/12/9999', 'DD/MM/YYYY');
BEGIN
  IF INSERTING THEN
    INSERT INTO DW_REF_MAIN.COUNTRY(COUNTRY_CODE, COUNTRY_NAME, IS_AREA, IS_EUROSYSTEM, IS_EU, IS_EEA, IS_G10, IS_EURO_PERIPHERY, IS_OECD_MEMBER, EXEMPT_FROM_CQR, LOSS_SHARING, NCB_NAME, REGION, A_VALID_FROM, A_VALID_TO, A_KEY)
    VALUES (:new.COUNTRY_CODE, :new.COUNTRY_NAME, :new.IS_AREA, :new.IS_EUROSYSTEM, :new.IS_EU, :new.IS_EEA, :new.IS_G10, :new.IS_EURO_PERIPHERY, :new.IS_OECD_MEMBER, :new.EXEMPT_FROM_CQR, :new.LOSS_SHARING, :new.NCB_NAME, :new.REGION, v_current_date, v_future_date, NULL);
    INSERT INTO REFERENCE.T_COUNTRY(COUNTRY_CODE, REF_AREA, COUNTRY_NAME, IS_AREA, IS_EUROSYSTEM, IS_EU, IS_EEA, IS_G10, IS_EURO_PERIPHERY, IS_OECD_MEMBER, EXEMPT_FROM_CQR, LOSS_SHARING, NCB_NAME, REGION, SORT_ORDER, BIZ_VALID_FROM, BIZ_VALID_TO, A_VALID_FROM, A_VALID_TO, A_KEY, A_MOPDB_LOAD_SET_FK)
    VALUES (:new.COUNTRY_CODE, :new.REPORTING_COUNTRY_CODE, :new.COUNTRY_NAME, :new.IS_AREA, :new.IS_EUROSYSTEM, :new.IS_EU, :new.IS_EEA, :new.IS_G10, :new.IS_EURO_PERIPHERY, :new.IS_OECD_MEMBER, :new.EXEMPT_FROM_CQR, :new.LOSS_SHARING, :new.NCB_NAME, :new.REGION, :new.SORT_ORDER, :new.BIZ_VALID_FROM, :new.BIZ_VALID_TO, v_current_date, v_future_date, NULL, -1);
  ELSIF DELETING THEN
    UPDATE DW_REF_MAIN.COUNTRY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(COUNTRY_NAME, :old.COUNTRY_NAME, 1, 0) = 1
    AND DECODE(IS_AREA, :old.IS_AREA, 1, 0) = 1
    AND DECODE(IS_EUROSYSTEM, :old.IS_EUROSYSTEM, 1, 0) = 1
    AND DECODE(IS_EU, :old.IS_EU, 1, 0) = 1
    AND DECODE(IS_EEA, :old.IS_EEA, 1, 0) = 1
    AND DECODE(IS_G10, :old.IS_G10, 1, 0) = 1
    AND DECODE(IS_EURO_PERIPHERY, :old.IS_EURO_PERIPHERY, 1, 0) = 1
    AND DECODE(IS_OECD_MEMBER, :old.IS_OECD_MEMBER, 1, 0) = 1
    AND DECODE(EXEMPT_FROM_CQR, :old.EXEMPT_FROM_CQR, 1, 0) = 1
    AND DECODE(LOSS_SHARING, :old.LOSS_SHARING, 1, 0) = 1
    AND DECODE(NCB_NAME, :old.NCB_NAME, 1, 0) = 1
    AND DECODE(REGION, :old.REGION, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    UPDATE REFERENCE.T_COUNTRY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(REF_AREA, :old.REPORTING_COUNTRY_CODE, 1, 0) = 1
    AND DECODE(COUNTRY_NAME, :old.COUNTRY_NAME, 1, 0) = 1
    AND DECODE(IS_AREA, :old.IS_AREA, 1, 0) = 1
    AND DECODE(IS_EUROSYSTEM, :old.IS_EUROSYSTEM, 1, 0) = 1
    AND DECODE(IS_EU, :old.IS_EU, 1, 0) = 1
    AND DECODE(IS_EEA, :old.IS_EEA, 1, 0) = 1
    AND DECODE(IS_G10, :old.IS_G10, 1, 0) = 1
    AND DECODE(IS_EURO_PERIPHERY, :old.IS_EURO_PERIPHERY, 1, 0) = 1
    AND DECODE(IS_OECD_MEMBER, :old.IS_OECD_MEMBER, 1, 0) = 1
    AND DECODE(EXEMPT_FROM_CQR, :old.EXEMPT_FROM_CQR, 1, 0) = 1
    AND DECODE(LOSS_SHARING, :old.LOSS_SHARING, 1, 0) = 1
    AND DECODE(NCB_NAME, :old.NCB_NAME, 1, 0) = 1
    AND DECODE(REGION, :old.REGION, 1, 0) = 1
    AND DECODE(SORT_ORDER, :old.SORT_ORDER, 1, 0) = 1
    AND DECODE(BIZ_VALID_FROM, :old.BIZ_VALID_FROM, 1, 0) = 1
    AND DECODE(BIZ_VALID_TO, :old.BIZ_VALID_TO, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;

  ELSIF UPDATING THEN
    UPDATE DW_REF_MAIN.COUNTRY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(COUNTRY_NAME, :old.COUNTRY_NAME, 1, 0) = 1
    AND DECODE(IS_AREA, :old.IS_AREA, 1, 0) = 1
    AND DECODE(IS_EUROSYSTEM, :old.IS_EUROSYSTEM, 1, 0) = 1
    AND DECODE(IS_EU, :old.IS_EU, 1, 0) = 1
    AND DECODE(IS_EEA, :old.IS_EEA, 1, 0) = 1
    AND DECODE(IS_G10, :old.IS_G10, 1, 0) = 1
    AND DECODE(IS_EURO_PERIPHERY, :old.IS_EURO_PERIPHERY, 1, 0) = 1
    AND DECODE(IS_OECD_MEMBER, :old.IS_OECD_MEMBER, 1, 0) = 1
    AND DECODE(EXEMPT_FROM_CQR, :old.EXEMPT_FROM_CQR, 1, 0) = 1
    AND DECODE(LOSS_SHARING, :old.LOSS_SHARING, 1, 0) = 1
    AND DECODE(NCB_NAME, :old.NCB_NAME, 1, 0) = 1
    AND DECODE(REGION, :old.REGION, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    UPDATE REFERENCE.T_COUNTRY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(REF_AREA, :old.REPORTING_COUNTRY_CODE, 1, 0) = 1
    AND DECODE(COUNTRY_NAME, :old.COUNTRY_NAME, 1, 0) = 1
    AND DECODE(IS_AREA, :old.IS_AREA, 1, 0) = 1
    AND DECODE(IS_EUROSYSTEM, :old.IS_EUROSYSTEM, 1, 0) = 1
    AND DECODE(IS_EU, :old.IS_EU, 1, 0) = 1
    AND DECODE(IS_EEA, :old.IS_EEA, 1, 0) = 1
    AND DECODE(IS_G10, :old.IS_G10, 1, 0) = 1
    AND DECODE(IS_EURO_PERIPHERY, :old.IS_EURO_PERIPHERY, 1, 0) = 1
    AND DECODE(IS_OECD_MEMBER, :old.IS_OECD_MEMBER, 1, 0) = 1
    AND DECODE(EXEMPT_FROM_CQR, :old.EXEMPT_FROM_CQR, 1, 0) = 1
    AND DECODE(LOSS_SHARING, :old.LOSS_SHARING, 1, 0) = 1
    AND DECODE(NCB_NAME, :old.NCB_NAME, 1, 0) = 1
    AND DECODE(REGION, :old.REGION, 1, 0) = 1
    AND DECODE(SORT_ORDER, :old.SORT_ORDER, 1, 0) = 1
    AND DECODE(BIZ_VALID_FROM, :old.BIZ_VALID_FROM, 1, 0) = 1
    AND DECODE(BIZ_VALID_TO, :old.BIZ_VALID_TO, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    INSERT INTO DW_REF_MAIN.COUNTRY(COUNTRY_CODE, COUNTRY_NAME, IS_AREA, IS_EUROSYSTEM, IS_EU, IS_EEA, IS_G10, IS_EURO_PERIPHERY, IS_OECD_MEMBER, EXEMPT_FROM_CQR, LOSS_SHARING, NCB_NAME, REGION, A_VALID_FROM, A_VALID_TO, A_KEY)
    VALUES (:new.COUNTRY_CODE, :new.COUNTRY_NAME, :new.IS_AREA, :new.IS_EUROSYSTEM, :new.IS_EU, :new.IS_EEA, :new.IS_G10, :new.IS_EURO_PERIPHERY, :new.IS_OECD_MEMBER, :new.EXEMPT_FROM_CQR, :new.LOSS_SHARING, :new.NCB_NAME, :new.REGION, v_current_date, v_future_date, NULL);
    INSERT INTO REFERENCE.T_COUNTRY(COUNTRY_CODE, REF_AREA, COUNTRY_NAME, IS_AREA, IS_EUROSYSTEM, IS_EU, IS_EEA, IS_G10, IS_EURO_PERIPHERY, IS_OECD_MEMBER, EXEMPT_FROM_CQR, LOSS_SHARING, NCB_NAME, REGION, SORT_ORDER, BIZ_VALID_FROM, BIZ_VALID_TO, A_VALID_FROM, A_VALID_TO, A_KEY, A_MOPDB_LOAD_SET_FK)
    VALUES (:new.COUNTRY_CODE, :new.REPORTING_COUNTRY_CODE, :new.COUNTRY_NAME, :new.IS_AREA, :new.IS_EUROSYSTEM, :new.IS_EU, :new.IS_EEA, :new.IS_G10, :new.IS_EURO_PERIPHERY, :new.IS_OECD_MEMBER, :new.EXEMPT_FROM_CQR, :new.LOSS_SHARING, :new.NCB_NAME, :new.REGION, :new.SORT_ORDER, :new.BIZ_VALID_FROM, :new.BIZ_VALID_TO, v_current_date, v_future_date, NULL, -1);

  END IF;
END;
/