SET TERM ^ ;

CREATE OR ALTER trigger ttbwhatsapp_web_serv_b01u for tbwhatsapp_web_serv
active before update position 2
AS
DECLARE VARIABLE WEB_SERV_ID INTEGER;
BEGIN

    IF (OLD.REMOVER = 0 AND NEW.REMOVER = 1) THEN
    BEGIN

        FOR
            SELECT
                A.ID
            
            FROM TBWHATSAPP_TAREFAS T
            INNER JOIN TBWHATSAPP_WEB_SERV A ON A.TAREFA_ID = T.ID
            WHERE T.GRUPO = NEW.TAREFA_ID
            AND A.SERVIDOR_ID = NEW.SERVIDOR_ID
    
        INTO
            :WEB_SERV_ID
    
        DO
        BEGIN
    
            UPDATE TBWHATSAPP_WEB_SERV AR SET AR.REMOVER = 1 WHERE AR.ID = :WEB_SERV_ID AND AR.SERVIDOR_ID = NEW.SERVIDOR_ID;
    
        END

    END

END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER trigger ttbwhatsapp_web_serv_b02d for tbwhatsapp_web_serv
active before delete position 2
AS
DECLARE VARIABLE WEB_SERV_ID INTEGER;
BEGIN

    FOR
        SELECT
            A.ID
        
        FROM TBWHATSAPP_TAREFAS T
        INNER JOIN TBWHATSAPP_WEB_SERV A ON A.TAREFA_ID = T.ID
        WHERE T.GRUPO = OLD.TAREFA_ID
        AND A.SERVIDOR_ID = OLD.SERVIDOR_ID

    INTO
        :WEB_SERV_ID

    DO
    BEGIN

        DELETE FROM TBWHATSAPP_WEB_SERV AR WHERE AR.ID = :WEB_SERV_ID AND AR.SERVIDOR_ID = OLD.SERVIDOR_ID;

    END

END
^

SET TERM ; ^

