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

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' GP INCLUIDO: '||NEW.GP_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBMODELO M WHERE M.CODIGO = NEW.GP_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 = 'GP'), HISTORICO);
    END
END;

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

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


