
SET SERVEROUTPUT ON;
BEGIN
  FOR i IN (SELECT 
              'alter '||object_type||' '||owner||'.'||object_name||' compile ' AS sql_scpt
            FROM dba_objects 
            WHERE 
                owner IN (SELECT username FROM dba_users WHERE oracle_maintained <> 'Y') 
            AND status='INVALID' 
            AND object_type IN ('PACKAGE BODY', 'TYPE', 'SYNONYM', 'TRIGGER', 'PROCEDURE', 'VIEW', 'FUNCTION')
            ) LOOP
    BEGIN
      EXECUTE IMMEDIATE i.sql_scpt;
      DBMS_OUTPUT.PUT_LINE(i.sql_scpt);
      EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('ERROR when executing: '||i.sql_scpt);
    END;
  END LOOP;
END;
/