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

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' COR INCLUIDA: '||NEW.COR_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBCOR M WHERE M.CODIGO = NEW.COR_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 = 'COR'), HISTORICO);
    END
END;


CREATE trigger ttbperfil_cor_b01d for tbperfil_cor
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' COR EXCLUIDA: '||OLD.COR_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBMODELO M WHERE M.CODIGO = OLD.COR_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 = 'COR'), HISTORICO);
    END
END;
