I need to update product_details table with unique ID(CHARGE_NUMBER) againt each batch_ID and for the next batch ID unique ID again will start with 1
I have write a code using sequence.
update product_details
set charge_number = charge_num_seq.nextval
process_id = i.batch_id ;
But there is one question "What if multiple concurrent transaction comes ? It may not give correct result"
CREATE OR REPLACE PROCEDURE cbf_update_adjustment_seq(p_profile_id character varying) AS
begin
for j in (select * from inv_gen_trans where profile_id = p_profile_id) loop
begin
update product_details
set adj_seq = update_adj_seq.nextval ,
charge_number = charge_num_seq.nextval
where
process_id = j.batch_id ;
ALTER SEQUENCE charge_num_seq RESTART WITH 1;
ALTER SEQUENCE update_adj_seq RESTART WITH 1;
ALTER SEQUENCE update_adj_seq RESTART WITH 1;
exception
when others then
dbms_output.put_line('Error:'||errmsg);
end;
end loop;
end
One profile having some batch ID 3 - 4 batch ID's in inv_gen_trans table and below are the expected output in product_details table
process_id |charge_number
---------------|--------------
20190430810985 |1
20190430810985 |2
20190430810986 |1
20190430810986 |2
20190430810987 |1