Home

GOTO should not be used to jump backwards

Rule description

  • GOTO should not be used to jump backwards

 

Non-compliant Code Example

DECLARE
  first_name  VARCHAR2(25);
  customer_id     NUMBER(6) := 120;
BEGIN
  <<get_customer_name>>
  SELECT FIRSTNAME INTO first_name
  FROM CUSTOMERS
  WHERE Id = customer_id;
  
  BEGIN
    DBMS_OUTPUT.PUT_LINE (first_name);
    customer_id := customer_id + 1;
 
    IF customer_id < 200 THEN
      GOTO get_customer_name;  -- Non compliant code (GOTO statement is jumping backwards)
    END IF;
  END;
END;

Compliant Code Example

DECLARE
  first_name  VARCHAR2(25);
  customer_id     NUMBER(6) := 120;
BEGIN
 
  
  BEGIN
    DBMS_OUTPUT.PUT_LINE (first_name);
    customer_id := customer_id + 1;
 
    IF customer_id < 200 THEN
      GOTO get_customer_name;  -- Compliant code (GOTO statement is jumping forward)
    END IF;

    <<get_customer_name>>
      SELECT FIRSTNAME INTO first_name
      FROM CUSTOMERS
      WHERE Id = customer_id;
  END;
END;
Visual Expert 2020
 VEPLSQLRULE20