G-4320
🆓Warning
Always label your loops.
Reason
It's a good alternative for comments to indicate the start and end of a named loop processing.
Example
Non-Compliant Example
declare
i pls_integer;
begin
i := 1;
while (i <= 10)
loop
i := i + 1;
end loop;
loop
exit when true;
end loop;
for i in 1..10
loop
sys.dbms_output.put_line(i);
end loop;
for r_employee in (select last_name from employees)
loop
sys.dbms_output.put_line(r_employee.last_name);
end loop;
end;
/Issues
| Line | Column | Message |
|---|---|---|
| 5 | 4 | |
| 8 | 8 | |
| 10 | 4 | |
| 12 | 8 | |
| 14 | 4 | |
| 17 | 8 | |
| 19 | 4 | |
| 22 | 8 |
Compliant Solution - ★★★★★
declare
i pls_integer := 0;
begin
<<while_loop>>
while (i <= 10)
loop
i := i + 1;
end loop while_loop;
<<basic_loop>>
loop
exit when true;
end loop basic_loop;
<<for_loop>>
for i in 1..10
loop
sys.dbms_output.put_line(i);
end loop for_loop;
<<process_employees>>
for r_employee in (select last_name from employees)
loop
sys.dbms_output.put_line(r_employee.last_name);
end loop process_employees;
end;
/References
- similar to plsql:LabelledStatementUsageCheck
The scope of plsql:LabelledStatementUsageCheck is limited to nested loops only.
- same as Trivadis G-4320