Home » PLSQL » How to Compile All Invalid Packages in Schema?

How to Compile All Invalid Packages in Schema?

Below is an example of PL/SQL program to compile all invalid package specifications and all invalid package bodies for the current user (SCHEMA) in Oracle. Also, if any of the invalid packages are not successfully compiled then it will print the package name for manual error checking.

Compile All Invalid Packages in Oracle Example

SET SERVEROUTPUT ON;

DECLARE
CURSOR c_pkg
IS
SELECT owner, object_type, object_name
FROM dba_objects
WHERE status = 'INVALID'
AND owner = USER
AND object_type LIKE 'PACKAGE%'
ORDER BY object_type;
BEGIN
FOR c IN c_pkg
LOOP
BEGIN
IF c.object_type = 'PACKAGE'
THEN
EXECUTE IMMEDIATE 'ALTER PACKAGE ' || c.owner || '.' || c.object_name || ' COMPILE';
ELSE
EXECUTE IMMEDIATE 'ALTER PACKAGE '
|| c.owner || '.' || c.object_name
|| ' COMPILE BODY';
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line(c.object_type || ' ' || c.object_name
|| ' is still invalid. Check the package manually to resolve.');
END;
END LOOP;
END;
/

Example Output (if any error):

PACKAGE EMP_PKG is still invalid. Check the package manually to resolve.
PACKAGE BODY EMP_PKG is still invalid. Check the package manually to resolve.
PL/SQL procedure successfully completed.

See also: