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:

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 20 years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.