We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Problem looping through FETCH.. BULK COLLECT.. LIMIT

queryanalyzer
on
Medium Priority
949 Views
Last Modified: 2008-01-09
Hello,
    I am having problem looping through a cursor using bulk collect.

declare
  type typVar is table of tab1.field1%type index by binary_integer
  arrayVar typVar;
  cursor curTemp is select field1 from tab1;

begin
  open curTemp;
  loop  -- outer
    FETCH curTemp BULK COLLECT into arrayVar  LIMIT 5;
    exit when curTemp%notfound;
    for i in 1..arrayVar.count loop
       /* Processing */
    end loop;
  end loop;  -- outer
end;

Here is what seems to be happening. If there are 13 records in the 'tab1' table, only 10 (5+5) gets processed. The remaining 3 is leftout.
It  looks like the 'limit 5' has something to do with it.
I assumed when FETCH is retrieving the last set of records, it would get whats left in the table, and that number need not be equal to the what LIMIT is set to.

In my case it is getting the first 5, processing it, then the next 5 and when it gets to the last 3 it is exiting from the loop.

Is this the normal behavior. Is so, how can i got around this problem.
I do need to limit the number of records fetched each time.

Thanks for your time.
Comment
Watch Question



try this:
...
exit when curTemp%ROWCOUNT = 0;


Hi
Try following:
I have just changed the position of ..
check out...


declare
  type typVar is table of tab1.field1%type index by binary_integer
  arrayVar typVar;
  cursor curTemp is select field1 from tab1;

begin
  open curTemp;
  loop  -- outer
    FETCH curTemp BULK COLLECT into arrayVar  LIMIT 5;
    for i in 1..arrayVar.count loop
       /* Processing */
    end loop;
    exit when curTemp%notfound;  <--- I have just changed the position of this exit...
  end loop;  -- outer
end;

Rgds,
HR

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks guys, I had to put the 'exit' at the end.
Apperciate your time.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.