G-4330: Always use a CURSOR FOR loop to process the complete cursor results unless you are using bulk operations.

Minor

Maintainability

Reason

It is easier for the reader to see, that the complete data set is processed. Using SQL to define the data to be processed is easier to maintain and typically faster than using conditional processing within the loop.

Since an exit statement is similar to a goto statement, it should be avoided whenever possible.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
declare
   cursor c_employee is
      select employee_id, last_name
        from employee;
   r_employee c_employee%rowtype;
begin
   open c_employee;

   <<read_employees>>
   loop
      fetch c_employee into r_employee;
      exit read_employees when c_employees%notfound;
      sys.dbms_output.put_line(r_employee.last_name);
   end loop read_employees;

   close c_employee;
end;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
declare
   cursor c_employee is
      select employee_id, last_name
        from employee;
begin
   <<read_employees>>
   for r_employee in c_employee
   loop
      sys.dbms_output.put_line(r_employee.last_name);
   end loop read_employees;
end;
/