G-2110: Try to use anchored declarations for variables, constants and types.

Major

Maintainability, Reliability

Reason

Changing the size of the database column last_name in the employees table from VARCHAR2(20) to VARCHAR2(30) will result in an error within your code whenever a value larger than the hard coded size is read from the table. This can be avoided using anchored declarations.

Example (bad)

CREATE OR REPLACE PACKAGE BODY my_package IS
   PROCEDURE my_proc IS
      l_last_name  VARCHAR2(20 CHAR);
      co_first_row CONSTANT INTEGER := 1;
   BEGIN
      SELECT e.last_name
        INTO l_last_name
        FROM employees e
       WHERE rownum = co_first_row;
   EXCEPTION
      WHEN no_data_found THEN NULL; -- handle no_data_found
      WHEN too_many_rows THEN NULL; -- handle too_many_rows (impossible)
   END my_proc;
END my_package;
/

Example (good)

CREATE OR REPLACE PACKAGE BODY my_package IS
   PROCEDURE my_proc IS
      l_last_name  employees.last_name%TYPE;
      co_first_row CONSTANT INTEGER := 1;
   BEGIN
      SELECT e.last_name
        INTO l_last_name
        FROM employees e
       WHERE rownum = co_first_row;
   EXCEPTION
      WHEN no_data_found THEN NULL; -- handle no_data_found
      WHEN too_many_rows THEN NULL; -- handle too_many_rows (impossible)
   END my_proc;
END my_package;
/