CREATE OR REPLACE TRIGGER DTULTATU_CONFTELACAIXA
BEFORE INSERT OR UPDATE OR DELETE
ON CONFTELACAIXA FOR EACH ROW
DECLARE
   V_NRCONFTELA VARCHAR2(05);
   V_CDFILIAL VARCHAR2(04);
   V_DTINIVIGENCIA VARCHAR2(10);
   V_COUNT NUMBER;
   V_DSCHAVE VARCHAR2(100);
BEGIN
   IF INSERTING OR UPDATING THEN
      V_NRCONFTELA := NVL(:NEW.NRCONFTELA, :OLD.NRCONFTELA);
      V_CDFILIAL := NVL(:NEW.CDFILIAL, :OLD.CDFILIAL);
      V_DTINIVIGENCIA := TO_CHAR(NVL(:NEW.DTINIVIGENCIA, :OLD.DTINIVIGENCIA), 'DD/MM/YYYY');
      V_DSCHAVE := V_CDFILIAL||'|'||V_NRCONFTELA||'|'||V_DTINIVIGENCIA;
      V_COUNT := 0;
      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM CTRLATUALTAB
          WHERE NMTABELA = 'CONFTELA'
            AND DSCHAVE = V_DSCHAVE;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_COUNT := 0;
      END;
      IF V_COUNT = 0 THEN
         INSERT INTO CTRLATUALTAB
            (NMTABELA, DSCHAVE, DTULTATU)
         VALUES
            ('CONFTELA', V_DSCHAVE, SYSDATE);
      ELSE
         UPDATE CTRLATUALTAB
            SET DTULTATU = SYSDATE
          WHERE NMTABELA = 'CONFTELA'
            AND DSCHAVE = V_DSCHAVE;
      END IF;
   END IF;
END;
/