CREATE OR REPLACE TRIGGER DTULTATU_TABEPRECGER
BEFORE INSERT OR UPDATE OR DELETE
ON TABEPRECGER FOR EACH ROW
DECLARE
   V_CDTABEPREC VARCHAR2(03);
   V_COUNT NUMBER;
   V_DSCHAVE VARCHAR2(100);
BEGIN
   IF INSERTING OR UPDATING THEN
      :NEW.DTULTATU := SYSDATE;
      V_CDTABEPREC := NVL(:NEW.CDTABEPREC, :OLD.CDTABEPREC);
      V_DSCHAVE := V_CDTABEPREC;
      V_COUNT := 0;
      BEGIN
         SELECT COUNT(*)
           INTO V_COUNT
           FROM CTRLATUALTAB
          WHERE NMTABELA = 'TABEPRECGER'
            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
            ('TABEPRECGER', V_DSCHAVE, SYSDATE);
      ELSE
         UPDATE CTRLATUALTAB
            SET DTULTATU = SYSDATE
          WHERE NMTABELA = 'TABEPRECGER'
            AND DSCHAVE = V_DSCHAVE;
      END IF;
   END IF;
   IF DELETING THEN
      INSERT INTO DELETECACHE (NMTABLECACHE, CDFILICACHE, DSKEYCACHE, DTULTATUCACH)
                       VALUES ('TABEPRECGER', NULL, 'CDTABEPREC='||:OLD.CDTABEPREC||';', SYSDATE);
   END IF;
END;
/

ALTER TABLE TABEPRECGER MODIFY DTULTATU DEFAULT SYSDATE
/

-- VERSAO --
BEGIN ATUVEROBJ('DTULTATU_TABEPRECGER','6.35.041'); END;
/

