create or replace TRIGGER LOG_FCT_RELACESTRUTURACONT BEFORE
    INSERT OR DELETE ON FCT_RELACESTRUTURACONT
    FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
    V_NRLOGDADOSCONT        NUMBER;
BEGIN

    IF INSERTING THEN
            IF ( NVL(:OLD.NRESTRUTURAM, 0) ) <> ( NVL(:NEW.NRESTRUTURAM, 0) ) THEN
                NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
                INSERT INTO FCT_LOGDADOSCONT (
                    NRLOGDADOSCONT,
                    NRSEQCONTRATO,
                    NRITEMOCONTRATO,
                    DSCAMPOALTERADO,
                    DSVALORANTIGO,
                    DSVALORNOVO,
                    NRORG,
                    DTALTERACAO,
                    CDOPERALTERACAO,
                    VALORANTIGO,
                    VALORNOVO
                ) VALUES (
                    V_NRLOGDADOSCONT,
                    :NEW.NRSEQCONTRATO,
                    NULL,
                    'Local de Atendimento Inserido',
                    NULL,
                    (SELECT NMESTRUTURAM FROM ESTRUTURAM WHERE NRESTRUTURAM = :NEW.NRESTRUTURAM AND NRORG = :NEW.NRORG),
                    :NEW.NRORG,
                    SYSDATE,
                    :NEW.CDOPERULTATU,
                    NULL,
                    :NEW.NRESTRUTURAM
                );
            END IF;
        END IF;
    IF DELETING THEN
         IF ( NVL(:OLD.NRESTRUTURAM, 0) ) <> ( NVL(:NEW.NRESTRUTURAM, 0) ) THEN
            NOVO_CODIGO_SC('FCT_LOGDADOSCONT', V_NRLOGDADOSCONT, 1);
            INSERT INTO FCT_LOGDADOSCONT (
                 NRLOGDADOSCONT,
                 NRSEQCONTRATO,
                 NRITEMOCONTRATO,
                 DSCAMPOALTERADO,
                 DSVALORANTIGO,
                 DSVALORNOVO,
                 NRORG,
                 DTALTERACAO,
                 CDOPERALTERACAO,
                 VALORANTIGO,
                 VALORNOVO
             ) VALUES (
                 V_NRLOGDADOSCONT,
                 :OLD.NRSEQCONTRATO,
                 NULL,
                 'Local de Atendimento Deletado',
                 (SELECT NMESTRUTURAM FROM ESTRUTURAM WHERE NRESTRUTURAM = :OLD.NRESTRUTURAM AND NRORG = :OLD.NRORG),
                 NULL,
                 :OLD.NRORG,
                 SYSDATE,
                 :OLD.CDOPERULTATU,
                 :OLD.NRESTRUTURAM,
                  NULL
             );
           END IF;
    END IF;
    COMMIT;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('LOG_FCT_RELACESTRUTURACONT','6.25.xxx'); END;
/