create or replace TRIGGER OU_REF.CAPITAL_KEY_H
BEFORE INSERT OR UPDATE OR DELETE ON OU_REF.CAPITAL_KEY
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.CAPITAL_KEY(COUNTRY_CODE, NCB_INDEX, CAPITAL_KEY_ADJ, RAR_NCB_NAME, CAPITAL_KEY_VALUE, PAID_UP_CAPITAL, A_VALID_FROM, A_VALID_TO, A_KEY)
    VALUES (:new.COUNTRY_CODE, :new.NCB_INDEX, NULL, :new.RAR_NCB_NAME, :new.CAPITAL_KEY_VALUE, :new.PAID_UP_CAPITAL, v_current_date, v_future_date, NULL);
    INSERT INTO REFERENCE.T_CAPITAL_KEY(COUNTRY_CODE, NCB_NAME, EUROSYSTEM_KEY, EUROSYSTEM_KEY_ECB, ESCB_KEY, ESCB_KEY_ECB, CAPITAL_KEY_VALUE, PAID_UP_CAPITAL, A_VALID_FROM, A_VALID_TO, A_KEY, A_MOPDB_LOAD_SET_FK)
    VALUES (:new.COUNTRY_CODE, :new.RAR_NCB_NAME, :new.EUROSYSTEM_KEY, :new.EUROSYSTEM_KEY_ECB, :new.ESCB_KEY, :new.ESCB_KEY_ECB, :new.CAPITAL_KEY_VALUE, :new.PAID_UP_CAPITAL, v_current_date, v_future_date, NULL,-1);
  ELSIF DELETING THEN
    UPDATE DW_REF_MAIN.CAPITAL_KEY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(NCB_INDEX, :old.NCB_INDEX, 1, 0) = 1
    AND DECODE(RAR_NCB_NAME, :old.RAR_NCB_NAME, 1, 0) = 1
    AND DECODE(CAPITAL_KEY_VALUE, :old.CAPITAL_KEY_VALUE, 1, 0) = 1
    AND DECODE(PAID_UP_CAPITAL, :old.PAID_UP_CAPITAL, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    UPDATE REFERENCE.T_CAPITAL_KEY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(NCB_NAME, :old.RAR_NCB_NAME, 1, 0) = 1
    AND DECODE(EUROSYSTEM_KEY, :old.EUROSYSTEM_KEY, 1, 0) = 1
    AND DECODE(EUROSYSTEM_KEY_ECB, :old.EUROSYSTEM_KEY_ECB, 1, 0) = 1
    AND DECODE(ESCB_KEY, :old.ESCB_KEY, 1, 0) = 1
    AND DECODE(ESCB_KEY_ECB, :old.ESCB_KEY_ECB, 1, 0) = 1
    AND DECODE(CAPITAL_KEY_VALUE, :old.CAPITAL_KEY_VALUE, 1, 0) = 1
    AND DECODE(PAID_UP_CAPITAL, :old.PAID_UP_CAPITAL, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;  
  ELSIF UPDATING THEN
    UPDATE DW_REF_MAIN.CAPITAL_KEY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(NCB_INDEX, :old.NCB_INDEX, 1, 0) = 1
    AND DECODE(RAR_NCB_NAME, :old.RAR_NCB_NAME, 1, 0) = 1
    AND DECODE(CAPITAL_KEY_VALUE, :old.CAPITAL_KEY_VALUE, 1, 0) = 1
    AND DECODE(PAID_UP_CAPITAL, :old.PAID_UP_CAPITAL, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;
    UPDATE REFERENCE.T_CAPITAL_KEY SET
      A_VALID_TO = v_current_date
    WHERE  
        1 = 1 
    AND DECODE(COUNTRY_CODE, :old.COUNTRY_CODE, 1, 0) = 1
    AND DECODE(NCB_NAME, :old.RAR_NCB_NAME, 1, 0) = 1
    AND DECODE(EUROSYSTEM_KEY, :old.EUROSYSTEM_KEY, 1, 0) = 1
    AND DECODE(EUROSYSTEM_KEY_ECB, :old.EUROSYSTEM_KEY_ECB, 1, 0) = 1
    AND DECODE(ESCB_KEY, :old.ESCB_KEY, 1, 0) = 1
    AND DECODE(ESCB_KEY_ECB, :old.ESCB_KEY_ECB, 1, 0) = 1
    AND DECODE(CAPITAL_KEY_VALUE, :old.CAPITAL_KEY_VALUE, 1, 0) = 1
    AND DECODE(PAID_UP_CAPITAL, :old.PAID_UP_CAPITAL, 1, 0) = 1
    AND A_VALID_TO = v_future_date
  ;  
    INSERT INTO DW_REF_MAIN.CAPITAL_KEY(COUNTRY_CODE, NCB_INDEX, CAPITAL_KEY_ADJ, RAR_NCB_NAME, CAPITAL_KEY_VALUE, PAID_UP_CAPITAL, A_VALID_FROM, A_VALID_TO, A_KEY)
    VALUES (:new.COUNTRY_CODE, :new.NCB_INDEX, NULL, :new.RAR_NCB_NAME, :new.CAPITAL_KEY_VALUE, :new.PAID_UP_CAPITAL, v_current_date, v_future_date, NULL);
    INSERT INTO REFERENCE.T_CAPITAL_KEY(COUNTRY_CODE, NCB_NAME, EUROSYSTEM_KEY, EUROSYSTEM_KEY_ECB, ESCB_KEY, ESCB_KEY_ECB, CAPITAL_KEY_VALUE, PAID_UP_CAPITAL, A_VALID_FROM, A_VALID_TO, A_KEY, A_MOPDB_LOAD_SET_FK)
    VALUES (:new.COUNTRY_CODE, :new.RAR_NCB_NAME, :new.EUROSYSTEM_KEY, :new.EUROSYSTEM_KEY_ECB, :new.ESCB_KEY, :new.ESCB_KEY_ECB, :new.CAPITAL_KEY_VALUE, :new.PAID_UP_CAPITAL, v_current_date, v_future_date, NULL,-1);
  END IF;
END;
/