do $$
declare
    _list refcursor;
    _item record;
    _id_op_fnc_blt integer;
    _id_cnt_bnc integer;
    _id_emp integer;
    _nr_doc integer;
begin
    -- Empresa
    _id_emp        := 1;
    
    -- Conta bancaria que gera boleto
    _id_cnt_bnc    := 6;
    
    -- Operacao financeira dos boletos
    _id_op_fnc_blt := 8;
    
    -- Numero do primeiro boleto
    _nr_doc       := 100;

    open _list for
        select *
        from
        (
                select 
                    tb_ct.*, 
                    tb_rem_blt__ct.id_rem_blt
                from tb_ct
                left outer join tb_rem_blt__ct on
                    (tb_ct.id_emp = tb_rem_blt__ct.id_emp and
                     tb_ct.id_ct = tb_rem_blt__ct.id_ct)
                where 
                    tb_ct.id_emp = _id_emp and
                    tb_ct.id_cnt_bnc = _id_cnt_bnc and
                    tb_ct.id_op_fnc = _id_op_fnc_blt and
                    tb_ct.stat = 'P' and
                    tb_ct.tp_ct = 'R'
         ) as tb
         where 
            id_rem_blt is null
        order by
            dt_emis, id_ct;
            
            
    fetch _list into _item;
    
    while (found) loop
        
        update tb_ct set nr_doc = _nr_doc
        where
            id_emp = _id_emp and
            id_ct = _item.id_ct;
        
        _nr_doc := _nr_doc + 1;
        
        fetch _list into _item;
    
    end loop;
    
end
$$;