If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Bulk collection issue with limit

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-10, 01:38
scharan07 scharan07 is offline
Registered User
 
Join Date: Jul 2009
Location: Mumbai
Posts: 21
Bulk collection issue with limit

Hi,

i written a piece of code in procedure to get a bulk collection data in to collection type with limit of 5. while iterating there has only 4 values in the cursor it is not able to retrive the data for those 4 records

Code:
CURSOR CURSOR_RESULTS IS 
  SELECT ID FROM ACC WHERE ISHIDDEN=1; 
  TYPE ID_TYPE IS TABLE OF ACC.ID%TYPE;
  LIST1 ID_TYPE;
BEGIN
    OPEN CURSOR_RESULTS;
    LOOP
    DBMS_OUTPUT.PUT_LINE('CURSUR ITERATION');
    FETCH CURSOR_RESULTS BULK COLLECT INTO LIST1 LIMIT 5;
    EXIT WHEN CURSOR_RESULTS%NOTFOUND;
      WALKER:= LIST1.FIRST;
      FOR I IN 1..LIST1.COUNT LOOP
        ID:=LIST1(WALKER);
        WALKER:=LIST1.NEXT(I);
        DBMS_OUTPUT.PUT_LINE('ID ITERATION'||ID);
      END LOOP;  
    END LOOP;
    CLOSE CURSOR_RESULTS;
Quote:
my total ids present in are 14 records which are 41,42,43,44,,45,46,47,48,49,1,2,3,51
but my out put is
Quote:
CURSUR ITERATION
ID ITERATION41
ID ITERATION42
ID ITERATION43
ID ITERATION44
ID ITERATION45
CURSUR ITERATION
ID ITERATION46
ID ITERATION47
ID ITERATION48
ID ITERATION49
ID ITERATION1
CURSUR ITERATION
after last "CURSUR ITERATION" i am expecting another 4 "ID ITERATION"s which are not coming.

because of limit 5, which it has only 4 records thats why it is not get the records into collection..?
Reply With Quote
  #2 (permalink)  
Old 09-01-10, 08:23
magicwand magicwand is offline
Registered User
 
Join Date: Mar 2010
Location: Vienna, Austria
Posts: 128
scharan07,

the line
Code:
EXIT WHEN CURSOR_RESULTS%NOTFOUND;
is in the wrong place.

The data of the last fetch is in your LIST1 table, but doesn't get printed, because the NOTFOUND is raised before the output - loop.

If you program it

Code:
BEGIN
    OPEN CURSOR_RESULTS;
    LOOP
      DBMS_OUTPUT.PUT_LINE('CURSUR ITERATION');
      FETCH CURSOR_RESULTS BULK COLLECT INTO LIST1 LIMIT 5;
      WALKER:= LIST1.FIRST;
      FOR I IN 1..LIST1.COUNT LOOP
        ID:=LIST1(WALKER);
        WALKER:=LIST1.NEXT(I);
        DBMS_OUTPUT.PUT_LINE('ID ITERATION'||ID);
      END LOOP;  
      EXIT WHEN CURSOR_RESULTS%NOTFOUND;
    END LOOP;
    CLOSE CURSOR_RESULTS;
END;
you will get the expected results.


(As always when no version is mentioned, I assume the latest, which is 11gR2 at the moment)
__________________
If A is a success in life, then A = x + y + z.
Work is x; y is play; and z is keeping your mouth shut. After all the years, I'm still working on the correct value for z.
(Albert Einstein)

Last edited by magicwand; 09-01-10 at 09:12. Reason: typos fixed, essential line highligted
Reply With Quote
  #3 (permalink)  
Old 09-02-10, 00:52
scharan07 scharan07 is offline
Registered User
 
Join Date: Jul 2009
Location: Mumbai
Posts: 21
Cool

Thanks magicwand it is working fine.

i am using oracle 10g version.
Reply With Quote
  #4 (permalink)  
Old 09-02-10, 08:25
magicwand magicwand is offline
Registered User
 
Join Date: Mar 2010
Location: Vienna, Austria
Posts: 128
scharan07,

one more thing:

You should enclose the FOR - loop in an IF statement to avoid an error if <#of output lines> mod 5 = 0;

I'd suggest:

Code:
      ...
      FETCH CURSOR_RESULTS BULK COLLECT INTO LIST1 LIMIT 5;
      IF LIST1.COUNT > 0 THEN
         WALKER:= LIST1.FIRST;
         FOR I IN 1..LIST1.COUNT LOOP
           ID:=LIST1(WALKER);
           WALKER:=LIST1.NEXT(I);
           DBMS_OUTPUT.PUT_LINE('ID ITERATION'||ID);
         END LOOP;  
      END IF;
      EXIT WHEN CURSOR_RESULTS%NOTFOUND;
      ...
__________________
If A is a success in life, then A = x + y + z.
Work is x; y is play; and z is keeping your mouth shut. After all the years, I'm still working on the correct value for z.
(Albert Einstein)
Reply With Quote
  #5 (permalink)  
Old 09-03-10, 01:24
scharan07 scharan07 is offline
Registered User
 
Join Date: Jul 2009
Location: Mumbai
Posts: 21
Yes. sure i got your point i will include that into my code

Thanks once again magicwand.
Reply With Quote
Reply

Tags
bulk collection, limit

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On