Sub topic 10
Reading Compilation Errors and Understanding Invalidation
This chapter assumes that you know basic SQL, anonymous PL/SQL blocks, and Oracle data types. You also need access to a schema where you can create procedures and query dictionary views.
Compilation happens when you create a subprogram
A stored procedure or function is compiled when you run CREATE OR REPLACE. Oracle checks the PL/SQL syntax, referenced names, and many data types. If compilation succeeds, the object becomes VALID.
CREATE OR REPLACE PROCEDURE show_message AS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello');
END show_message;
/If compilation fails, Oracle still creates or replaces the object, but its status becomes INVALID. The object cannot run successfully until you correct the errors and compile it again.
In SQL*Plus or SQLcl, you can display errors immediately after compilation:
SHOW ERRORS PROCEDURE show_message;You can also query the data dictionary. A data dictionary view is an Oracle-provided view containing metadata about database objects.
SELECT line, position, text
FROM user_errors
WHERE name = 'SHOW_MESSAGE'
AND type = 'PROCEDURE'
ORDER BY sequence;LINE and POSITION identify the approximate location. TEXT describes the problem. Object names are normally stored in uppercase unless they were created with quoted identifiers.
Fix errors and recompile
Suppose a procedure refers to a column that does not exist:
CREATE OR REPLACE PROCEDURE print_employee_name (
p_employee_id IN NUMBER
) AS
v_name VARCHAR2(100);
BEGIN
SELECT first_name || ' ' || middle_name
INTO v_name
FROM employees
WHERE employee_id = p_employee_id;
DBMS_OUTPUT.PUT_LINE(v_name);
END print_employee_name;
/If MIDDLE_NAME is not a column in EMPLOYEES, Oracle reports a compilation error. Correct the source and run CREATE OR REPLACE again. Recompilation replaces the invalid stored source with the corrected version.
Dependencies can invalidate objects
A dependency exists when one object relies on another object. A procedure that selects from a table depends on that table. A procedure that calls a function depends on that function.
flowchart TD
T[EMPLOYEES table] --> P[PRINT_EMPLOYEE_NAME procedure]
F[GET_FULL_NAME function] --> Q[REPORT procedure]When a referenced object changes, Oracle may mark dependent objects INVALID. For example, dropping or changing a table column can invalidate a procedure that uses that column. Oracle attempts automatic recompilation when an invalid object is called. If recompilation fails, the call raises an error.
Check object status with USER_OBJECTS:
SELECT object_name, object_type, status
FROM user_objects
WHERE object_name IN ('PRINT_EMPLOYEE_NAME', 'GET_FULL_NAME')
ORDER BY object_type, object_name;USER_DEPENDENCIES shows recorded relationships:
SELECT name, type, referenced_name, referenced_type
FROM user_dependencies
WHERE name = 'PRINT_EMPLOYEE_NAME';Practical workflow
- Compile the procedure or function.
- Check
SHOW ERRORSorUSER_ERRORS. - Fix the first reported error.
- Compile again.
- Confirm
STATUS = 'VALID'. - Run a focused test call.
Exercise
What does an INVALID status mean, and which view can show compilation details?
Show answer
It means Oracle could not currently compile the object successfully, or a dependency changed. USER_ERRORS shows the compilation details.