Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

%NOTFOUND - Question on why the EXIT condition is placed right after FETCH

Rodney ChanDec 16 2010 — edited Dec 16 2010
So i have a procedure that uses a cursor as well as a loop to output the values as follows

LOOP

/* Retrieve each row of the result of the above query into PL/SQL variables: */

FETCH AREA_CUR INTO P_ID, P_LAST_NAME, P_DEPTH;

/* If there are no more rows to fetch, exit the loop: */

EXIT WHEN AREA_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Student ID: '||P_ID);
DBMS_OUTPUT.PUT_LINE('Student Last Name: '||P_LAST_NAME);
DBMS_OUTPUT.PUT_LINE('Depth: '||P_DEPTH);
END LOOP;

Right now, if i were to run my procedure i would get output of

Schmidt
Burha

Which is expected

But i noticed if i were to switch the EXIT statement to the end just before the end loop i get slightly different results

LOOP

/* Retrieve each row of the result of the above query into PL/SQL variables: */

FETCH AREA_CUR INTO P_ID, P_LAST_NAME, P_DEPTH;
DBMS_OUTPUT.PUT_LINE('Student ID: '||P_ID);
DBMS_OUTPUT.PUT_LINE('Student Last Name: '||P_LAST_NAME);
DBMS_OUTPUT.PUT_LINE('Depth: '||P_DEPTH);

/* If there are no more rows to fetch, exit the loop: */

EXIT WHEN AREA_CUR%NOTFOUND;

END LOOP;

I then get the results of

Schmidt
Buhra
Burha

From what I understand after a FETCH statement is issued, it will take the current value of the row its in and then also move to the next line.

Why would it pick up Burha twice?

Since the FETCH statement moves to the next line i would expect the output to look more like

Schmidt
Buhra
<Blank> (as this would be the newest line)

Am i misunderstanding this?
This post has been answered by Ora on Dec 16 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 13 2011
Added on Dec 16 2010
2 comments
471 views