SET TERM ^ ;

CREATE OR ALTER trigger ttbavaliacao_des_mod_fator_b1u for tbavaliacao_des_mod_fator
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_FATOR_ID <> NEW.AVALIACAO_DES_FATOR_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' FATOR:'||OLD.AVALIACAO_DES_FATOR_ID||'->'||NEW.AVALIACAO_DES_FATOR_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_MODELO', OLD.AVALIACAO_DES_MODELO_ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbavaliacao_des_mod_form_b1u for tbavaliacao_des_mod_formacao
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_FORMACAO_ID <> NEW.AVALIACAO_DES_FORMACAO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' FORMACAO:'||OLD.AVALIACAO_DES_FORMACAO_ID||'->'||NEW.AVALIACAO_DES_FORMACAO_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_MODELO', OLD.AVALIACAO_DES_MODELO_ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbavaliacao_des_mod_resu_b1u for tbavaliacao_des_mod_resumo
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_RESUMO_ID <> NEW.AVALIACAO_DES_RESUMO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' RESUMO:'||OLD.AVALIACAO_DES_RESUMO_ID||'->'||NEW.AVALIACAO_DES_RESUMO_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_MODELO', OLD.AVALIACAO_DES_MODELO_ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END^

SET TERM ; ^

/************************************************************************************************/
/************************************************************************************************/

SET TERM ^ ;

CREATE OR ALTER trigger ttbaval_des_resp_base_ccust_b1u for tbavaliacao_des_resp_base_ccust
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_RESP_BASE_ID <> NEW.AVALIACAO_DES_RESP_BASE_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' BASE:'||OLD.AVALIACAO_DES_RESP_BASE_ID||'->'||NEW.AVALIACAO_DES_RESP_BASE_ID;
    END

    IF (OLD.CCUSTO_CODIGO <> NEW.CCUSTO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' CCUSTO:'||OLD.CCUSTO_CODIGO||'->'||NEW.CCUSTO_CODIGO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_RESP_BASE', OLD.AVALIACAO_DES_RESP_BASE_ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END
^

SET TERM ; ^

