Home
Loop start and end labels should match
Description
The rule "Loop start and end labels should match" states that when writing a PL/SQL code block that contains a loop, the labels used to denote the start and end of the loop should be the same. This is important to ensure that the loop is executed correctly and that the code runs as expected. By using the same label for the start and end of the loop, it will be easier to identify the start and end points of the loop and to debug any issues that may arise.
Key Benefits
- Eliminates Syntax Errors: Loop start and end labels should match to ensure that the loop is properly closed and the code runs without any syntax errors.
- Improves Readability: By matching the loop start and end labels, the code becomes more organized and easier to read.
- Makes Debugging Easier: By having the loop start and end labels match, it is easier to identify any errors in the loop and debug them quickly.
Non-compliant Code Example
DECLARE
id customers.id%type;
name customer.name%type;
address customers.address%type;
CURSOR customers_c is
SELECT id, name, address FROM customers;
BEGIN
OPEN customers_c;
<<INNER_LOOP>> --Non compliant code (Loop start and end labels are not matching)
LOOP
FETCH customers_c into id, name, address;
EXIT WHEN customers_c%notfound;
dbms_output.put_line(id || ' ' || name || ' ' || address);
END LOOP INNERONE_LOOP;
CLOSE customers_c;
END;
Compliant Code Example
DECLARE
id customers.id%type;
name customer.name%type;
address customers.address%type;
CURSOR customers_c is
SELECT id, name, address FROM customers;
BEGIN
OPEN customers_c;
<<INNER_LOOP>> --Compliant code (Loop start and end labels are matching)
LOOP
FETCH customers_c into id, name, address;
EXIT WHEN customers_c%notfound;
dbms_output.put_line(id || ' ' || name || ' ' || address);
END LOOP INNER_LOOP;
CLOSE customers_c;
END;