I've been writing basic web apps for a year (for an Oracle db), and since the functions are pretty simple, most of us stick with regular FOR loops to get our data:
for i in (select * from STUDENTS) loop
htp.prn(i.student_last_name || ', ' || i.student_first_name || ' ' || i.student_dob);
end loop;
But, cursors seem to be the 'right' way to do things. I can find lots of information on what cursors are and different ways to loop through them, but I can't find a solid reason why to use them over regular FOR loops.
Is it dependent on the needs of the procedure? Are there inherent advantages I should be aware of?
Best Answer
A cursor can be explicit or implicit, and either type can be used in a FOR loop. There are really two aspects to your question.
Why use an explicit cursor FOR loop over an implicit cursor FOR loop?
Why use a loop with a FETCH rather than a FOR loop that doesn’t have an explicit FETCH?
Here is some useful information from the documentation.
Example of Implicit Cursor FOR LOOP
Example of Explicit Cursor FOR LOOP
Implicit Cursor
Explicit Cursor
Cursor FOR LOOP Statements