CREATE trigger ttbapi_familia_b02i for tbapi_familia
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'FAMILIA INCLUIDA:'||NEW.FAMILIA_ID||' - '||COALESCE((SELECT FIRST 1 A.DESCRICAO FROM TBFAMILIA A WHERE A.CODIGO = NEW.FAMILIA_ID), '');
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',NEW.API_ID, HISTORICO);
    END
end;



CREATE trigger ttbapi_familia_b01d for tbapi_familia
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'FAMILIA EXCLUIDA:'||OLD.FAMILIA_ID||' - '||COALESCE((SELECT FIRST 1 A.DESCRICAO FROM TBFAMILIA A WHERE A.CODIGO = OLD.FAMILIA_ID), '');
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',OLD.API_ID, HISTORICO);
    END
end;

