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.PERFIL_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'FAMILIA: '||FN_LPAD(NEW.FAMILIA_ID, 4, '0')||' INCLUIDA';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', COALESCE((SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = NEW.PERFIL_ID AND P.TABELA = 'PED'), 0), 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.PERFIL_ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' FAMILIA: '||FN_LPAD(OLD.FAMILIA_ID, 4, '0')||' EXCLUIDA';
    END

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

SET TERM ; ^

