CREATE trigger ttbperfil_up_b02i for tbperfil_up
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' UP INCLUIDA: '||NEW.UP_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBMODELO M WHERE M.CODIGO = NEW.UP_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL',(SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = NEW.PERFIL AND P.TABELA = 'UP'), HISTORICO);
    END
END;


CREATE OR ALTER trigger ttbperfil_up_b01d for tbperfil_up
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' UP EXCLUIDA: '||OLD.UP_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBMODELO M WHERE M.CODIGO = OLD.UP_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL',(SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = OLD.PERFIL AND P.TABELA = 'UP'), HISTORICO);
    END
END;

