? Cursor FOR loop

Cursor FOR loop

Top  Previous  Next

The Cursor FOR Loop

 

BEGIN

FOR rec_cust IN ( SELECT cust_num FROM customer )

LOOP

   DBMS_OUTPUT.PUT_LINE( rec_cust.cust_num);

END LOOP;

END;

FOR EACH customer NO-LOCK:

DISPLAY customer.cust_num.

END.

 

Here we have our beloved FOR EACH loop in Progress.

Note Oracle automatically declares rec_cust variable, declares anonymous cursor for SELECT and opens it on the first iteration, fetches records until SQL%NOTFOUND and finally closes it upon exiting the loop. As with numeric FOR LOOP rec_cust is local and could not be referenced outside the loop.