Skip to Main Content
  • Questions
  • update using bulk collect in plsql with exit when statement ,plsql script to run code in batches of 500 with some update statement

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, sona.

Asked: February 12, 2016 - 6:33 am UTC

Last updated: February 15, 2016 - 11:25 am UTC

Version: oracle 10.1.2

Viewed 1000+ times

You Asked

here is a table having 2000 records i need to update the table with some given condition update tablename set counter=1 where counter=null and rownum<500 for first execution so that counter should be 4 at last of 2000 rows execution,EXIT WHEN counter > 100; -- LOOP until condition is met loop will execute some sql update tablename set counter=1 where counter=null and rownum<100 the script will have to run until the conditions of that update state meant are met , I want to run a function that does that automatically for me until the condition is complete(condition is update statement)

and Connor said...

I'm not entirely sure what you are asking, but I'll take a stab.

If you mean you want to run an update in batches until nothing is left to do, you can do this:

loop
  update T 
  set x = [new value]
  where [your conditions]
  and  x != [new value]
  and   rownmum < 500;

  exit when sql%notfound;
end loop;



The "and x != [new value]" is important to ensure you dont repeatedly update the same values.

Hope this helps.

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Thanks Connor

sona sh, February 12, 2016 - 11:33 am UTC

Thanks for answering ,I will explain my requirement again.
I have to write a plsql procedure or script which update the update statement and set one of the column say indexnew=1,
the whole block of code should run for 4 times if the record is 2000.and i want to run it in n(4 in this case) run.

2000 record/500 = 4 count.
each time the inndexnew should be incremented by 1.

update
table set indexnew = 1
where column1 is not null
and indexnew is null
and rownum < 500
;
commit;
I have to manually increment the following:
set attachment = 2
so next run will be set attachment = 3
and the run after that will be set attachment = 4

what i meant is like a procedure/function which can increment the count value everything it get executed.

I am trying to batch up the records into batches of 500
I just want to launch a script that does this for me automatically - rather than me having to increment the batch number.

Hope the requirement is clear now.

Please reply and can you plz give me whole code for the same.


Thanks Connor

sona sh, February 12, 2016 - 11:33 am UTC

Thanks for answering ,I will explain my requirement again.
I have to write a plsql procedure or script which update the update statement and set one of the column say indexnew=1,
the whole block of code should run for 4 times if the record is 2000.and i want to run it in n(4 in this case) run.

2000 record/500 = 4 count.
each time the inndexnew should be incremented by 1.

update
table set indexnew = 1
where column1 is not null
and indexnew is null
and rownum < 500
;
commit;
I have to manually increment the following:
set attachment = 2
so next run will be set attachment = 3
and the run after that will be set attachment = 4

what i meant is like a procedure/function which can increment the count value everything it get executed.

I am trying to batch up the records into batches of 500
I just want to launch a script that does this for me automatically - rather than me having to increment the batch number.

Hope the requirement is clear now.

Please reply and can you plz give me whole code for the same.


Connor McDonald
February 13, 2016 - 5:07 am UTC

Something like this ?


declare
  l_counter int := 1;
begin
--
-- if you always have to pick up where you left off then have this
-- (assuming this is a single user operation)
--
select max(counter)+1 into l_counter from T;

loop
  update T 
  set indexnew = l_counter
  where [your conditions]
  and  indexnew is null
  and   rownmum <= 500;

  exit when sql%notfound;
  l_counter := l_counter + 1;
end loop;
end;



Thanks Connor

sona sh, February 15, 2016 - 10:19 am UTC

THANKS FOR ANSWERING,IT WAS A GREAT HELP

Thanks Connor

sona sh, February 15, 2016 - 10:21 am UTC

HI CONNOR,

IT WILL BE REALLY GREAT IF YOU CAN GUIDE ME TO IMPROVE MY PLSQL CODING,
OR IF YOU CAN SHARE SOME LINK FROM WHERE I CAN LEARN IT SOON.
IT IS VERY MUCH REQUIRED IN MY PROJECT.

THANKS
CONNOR
Chris Saxon
February 15, 2016 - 10:31 am UTC

Thanks Chris

sona sh, February 15, 2016 - 10:43 am UTC

Thank a ton Chris for sharing the link.It will be a great help for me.
Connor McDonald
February 15, 2016 - 11:25 am UTC

Glad we could help