SET TERM ^ ;

CREATE OR ALTER trigger ttbperfil_familia_desconto_b02i for tbperfil_familia_desconto
active before insert position 9
AS
declare variable HISTORICO VARCHAR(500);
begin
    HISTORICO = '';

    IF(NEW.FAMILIA_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'FAMILIA: '||FN_LPAD(NEW.FAMILIA_ID, 4, '0')||' INCLUÍDA';
    END

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

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbperfil_familia_desconto_b01d for tbperfil_familia_desconto
active before delete position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF(OLD.FAMILIA_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' FAMILIA: '||FN_LPAD(OLD.FAMILIA_ID, 4, '0')||' EXCLUÍDA';
    END

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

SET TERM ; ^