ALTER TABLE TBCASO_ENVOLVIDOS
    ADD MSG_PENDENTE INTEGER DEFAULT 0;

COMMENT ON COLUMN TBCASO_ENVOLVIDOS.MSG_PENDENTE IS
'Qtd de mensagens não lidas';


UPDATE TBCASO_ENVOLVIDOS A SET A.MSG_PENDENTE = 0 WHERE A.MSG_PENDENTE IS NULL;


/*****************************************************************************/
/*****************************************************************************/

SET TERM ^ ;

CREATE OR ALTER trigger ttbcaso_feed_a01i for tbcaso_feed
active after insert position 1
AS
BEGIN

    UPDATE TBCASO_ENVOLVIDOS A
        SET A.MSG_PENDENTE = (COALESCE(A.MSG_PENDENTE, 0) + 1)
        WHERE A.CASO_ID = NEW.CASO_ID
        AND A.PAINEL_ID = NEW.PAINEL_ID
        AND A.USUARIO_ID <> NEW.USUARIO_ID;

END
^

SET TERM ; ^

