SET TERM ^ ;

CREATE trigger ttbimplantacao_detalhe_b02d for tbimplantacao_detalhe
active before delete position 0
AS
begin
    IF(OLD.ID > 0) THEN
    BEGIN
        DELETE FROM TBIMPLANTACAO_TELAS WHERE IMPLANTACAO_DETALHE_ID = OLD.ID;
    END
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbimplantacao_fases_b01u for tbimplantacao_fases
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
begin
    HISTORICO = '';

    IF(OLD.DATA_INICIO IS DISTINCT FROM NEW.DATA_INICIO) THEN
    BEGIN
        HISTORICO = HISTORICO||' FASE: '||NEW.DESCRICAO||' | DT.INICIO: '||COALESCE(FORMATDATE(OLD.DATA_INICIO),'')||' -> '||FORMATDATE(NEW.DATA_INICIO);
    END

    IF(OLD.DATA_FIM IS DISTINCT FROM NEW.DATA_FIM) THEN
    BEGIN
        HISTORICO = HISTORICO||' FASE: '||NEW.DESCRICAO||' | DT.FIM: '||COALESCE(FORMATDATE(OLD.DATA_FIM),'')||' -> '||FORMATDATE(NEW.DATA_FIM);
    END

    IF(HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBIMPLANTACAO', NEW.IMPLANTACAO_ID, HISTORICO);
    END
end
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbimplantacao_b02d for tbimplantacao
active before delete position 0
AS
begin
    IF(OLD.ID > 0) THEN
    BEGIN
        DELETE FROM TBIMPLANTACAO_DETALHE T WHERE T.IMPLANTACAO_ID = OLD.ID;
        DELETE FROM TBIMPLANTACAO_FASES T WHERE T.IMPLANTACAO_ID = OLD.ID;
        DELETE FROM TBIMPLANTACAO_DESENVOLVIMENTO T WHERE T.IMPLANTACAO_ID = OLD.ID;
    END
end
^

SET TERM ; ^