-- drop function if exists par.set_trans_credito_debito(integer) create or replace function par.set_trans_credito_debito(i_odeid integer) returns void language plpgsql as $$ declare romid_array integer[]:= ARRAY(SELECT romid FROM par.responsaveisomisso where odeid=i_odeid order by dtfimmandato); valor_debito numeric(20,2); data_debito timestamp; begin FOR i IN 1..(array_length(romid_array,1)-1) LOOP if ( (select coalesce(valorcredito, '0.00') from par.responsaveisomisso where romid = romid_array[i] ) > 0 ) then select valorcredito , datacredito into valor_debito, data_debito from par.responsaveisomisso where romid = romid_array[i]; update par.responsaveisomisso set datadebito = dtiniciomandato, valordebito = valor_debito where romid= romid_array[i+1]; end if; END LOOP; update par.responsaveisomisso set romstatus = 'A', carga = 'CREDITOVIRADEBITO' where odeid = i_odeid; end; $$;