G-3140: Try to use anchored records as targets for your cursors.

Major

Maintainability, Reliability

Reason

Using cursor-anchored records as targets for your cursors results enables the possibility of changing the structure of the cursor without regard to the target structure.

Example (bad)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
declare
   cursor c_employee is
      select employee_id, first_name, last_name
        from employee;
   l_employee_id employee.employee_id%type;
   l_first_name  employee.first_name%type;
   l_last_name   employee.last_name%type;
begin
   open c_employee;
   fetch c_employee into l_employee_id, l_first_name, l_last_name;
   <<process_employee>>
   while c_employee%found
   loop
      -- do something with the data
      fetch c_employee into l_employee_id, l_first_name, l_last_name;
   end loop process_employee;
   close c_employee;
end;
/

Example (good)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
declare
   cursor c_employee is
      select employee_id, first_name, last_name
        from employee;
   r_employee c_employee%rowtype;
begin
   open c_employee;
   fetch c_employee into r_employee;
   <<process_employee>>
   while c_employee%found
   loop
      -- do something with the data
      fetch c_employee into r_employee;
   end loop process_employee;
   close c_employee;
end;
/