Sub topic 3
Reading compilation errors and understanding invalidation
Oracle compiles a stored subprogram when you run its CREATE statement. If the code has errors, Oracle still saves the object, but marks it INVALID so it cannot run. This chapter assumes you know CREATE PROCEDURE, CREATE FUNCTION, and the USER_ dictionary views.
Reading compilation errors
Run SHOW ERRORS in SQL*Plus, or query USER_ERRORS directly:
SELECT line, position, text
FROM user_errors
WHERE name = 'RAISE_SALARY'
AND type = 'PROCEDURE'
ORDER BY sequence;Fix the lowest sequence first, because later errors often follow from the first one.
Invalidation and dependencies
A subprogram depends on the objects it references, such as tables, views, and other subprograms. When one of those objects changes, Oracle marks the subprogram INVALID. The next call tries to recompile it. If recompilation fails, the call raises an error.
Granting EXECUTE and choosing AUTHID
By default, a subprogram runs with its owner's privileges (definer's rights). Add AUTHID CURRENT_USER to use invoker's rights, which checks privileges against the calling user.
CREATE OR REPLACE FUNCTION tax_rate_for (
p_country_code IN VARCHAR2
) RETURN NUMBER
AUTHID CURRENT_USER
DETERMINISTIC
IS
BEGIN
RETURN CASE p_country_code
WHEN 'DE' THEN 0.19
WHEN 'FR' THEN 0.20
ELSE 0
END;
END tax_rate_for;
/
GRANT EXECUTE ON tax_rate_for TO app_user;Packages and overloading
A package groups related subprograms into a specification and a body. Overloading gives two subprograms the same name with different parameter types or counts. Oracle cannot tell overloads apart by return type alone.
DETERMINISTIC, RESULT_CACHE, and NOCOPY
DETERMINISTICpromises that the same inputs always give the same result. A false promise returns wrong values.RESULT_CACHEstores results in the shared pool across sessions. Use it for slow, read-mostly lookups.NOCOPYasks Oracle to pass an OUT or IN OUT parameter by reference instead of copying it. Oracle may ignore the hint.
Autonomous transactions
PRAGMA AUTONOMOUS_TRANSACTION lets a subprogram commit or roll back without affecting the caller's transaction. Use it for logging and auditing. The subprogram must issue its own COMMIT.
Pipelined table functions
A pipelined table function returns rows one at a time with PIPE ROW. SQL reads them through TABLE(). The function requires a schema-level collection type.
Design, naming, and testing guidelines
- Prefix parameters with
p_and variables withv_. - Anchor types with
%TYPEso they follow the column definition. - Give each subprogram one job.
- Test each change with an anonymous block that checks the result.
- After a deployment, check
USER_OBJECTSfor INVALID status.
Exercise
After you drop a column that a procedure references, the procedure shows INVALID. How do you find out why it fails to recompile, and what do you do next?
Show answer
Query USERERRORS for the procedure's name and type to read the error messages. Fix the code so it no longer references the dropped column, then run ALTER PROCEDURE procedurename COMPILE to recompile it and confirm the status is VALID in USER_OBJECTS.