SET TERM ^ ;

CREATE OR ALTER trigger ttbrelatorio_web_serv_b02i for tbrelatorio_web_serv
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE RELATORIO VARCHAR(500);
BEGIN

    HISTORICO = '';
    RELATORIO = COALESCE((SELECT FIRST 1 R.DESCRICAO FROM TBRELATORIO_WEB R WHERE R.ID = NEW.RELATORIO_ID),'');

    IF ( NEW.SERVIDOR_ID > 0 AND NEW.ID > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||' RELATORIO INCLUIDO: '||NEW.RELATORIO_ID||' - '||RELATORIO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBSERVIDOR',NEW.SERVIDOR_ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger tbrelatorio_web_serv_b01d for tbrelatorio_web_serv
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE RELATORIO VARCHAR(500);
BEGIN

    HISTORICO = '';
    RELATORIO = COALESCE((SELECT FIRST 1 R.DESCRICAO FROM TBRELATORIO_WEB R WHERE R.ID = OLD.RELATORIO_ID),'');

    IF ( OLD.SERVIDOR_ID > 0 AND OLD.ID > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||' RELATORIO EXCLUIDO: '||OLD.RELATORIO_ID||' - '||RELATORIO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBSERVIDOR',OLD.SERVIDOR_ID, HISTORICO);
    END
END
^

SET TERM ; ^


SET TERM ^ ;

CREATE trigger ttbservidor_b01d for tbservidor
active before delete position 0
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBRELATORIO_WEB_SERV WHERE SERVIDOR_ID = OLD.ID;
   END
END
^

SET TERM ; ^


