Compile all Invalids Manually in Oracle Database


Script:

DECLARE
   CURSOR invalid_objects_cur IS
      SELECT object_type, object_name, owner
      FROM dba_objects
      WHERE status = 'INVALID'
      AND object_type IN ('PACKAGE', 'PACKAGE BODY', 'PROCEDURE', 'FUNCTION', 'VIEW', 
                          'TRIGGER', 'SYNONYM', 'TYPE', 'TYPE BODY', 'MATERIALIZED VIEW');
BEGIN
   FOR obj IN invalid_objects_cur LOOP
      BEGIN
         -- Compile the invalid object
         IF obj.object_type = 'PACKAGE' THEN
            EXECUTE IMMEDIATE 'ALTER PACKAGE ' || obj.owner || '.' || obj.object_name || ' COMPILE';
         ELSIF obj.object_type = 'PACKAGE BODY' THEN
            EXECUTE IMMEDIATE 'ALTER PACKAGE ' || obj.owner || '.' || obj.object_name || ' COMPILE BODY';
         ELSIF obj.object_type = 'PROCEDURE' THEN
            EXECUTE IMMEDIATE 'ALTER PROCEDURE ' || obj.owner || '.' || obj.object_name || ' COMPILE';
         ELSIF obj.object_type = 'FUNCTION' THEN
            EXECUTE IMMEDIATE 'ALTER FUNCTION ' || obj.owner || '.' || obj.object_name || ' COMPILE';
         ELSIF obj.object_type = 'VIEW' THEN
            EXECUTE IMMEDIATE 'ALTER VIEW ' || obj.owner || '.' || obj.object_name || ' COMPILE';
         ELSIF obj.object_type = 'TRIGGER' THEN
            EXECUTE IMMEDIATE 'ALTER TRIGGER ' || obj.owner || '.' || obj.object_name || ' COMPILE';
         ELSIF obj.object_type = 'SYNONYM' THEN
            -- Synonyms cannot be compiled, so log a message
            DBMS_OUTPUT.put_line('Synonym ' || obj.owner || '.' || obj.object_name || ' cannot be compiled, consider recreating.');
         ELSIF obj.object_type = 'TYPE' THEN
            EXECUTE IMMEDIATE 'ALTER TYPE ' || obj.owner || '.' || obj.object_name || ' COMPILE';
         ELSIF obj.object_type = 'TYPE BODY' THEN
            EXECUTE IMMEDIATE 'ALTER TYPE ' || obj.owner || '.' || obj.object_name || ' COMPILE BODY';
         ELSIF obj.object_type = 'MATERIALIZED VIEW' THEN
            EXECUTE IMMEDIATE 'ALTER MATERIALIZED VIEW ' || obj.owner || '.' || obj.object_name || ' COMPILE';
         ELSE
            DBMS_OUTPUT.put_line('Unsupported object type: ' || obj.object_type || ' for object ' || obj.owner || '.' || obj.object_name);
         END IF;
      EXCEPTION
         WHEN OTHERS THEN
            DBMS_OUTPUT.put_line('Error compiling ' || obj.object_type || ' ' || obj.owner || '.' || obj.object_name || ': ' || SQLERRM);
      END;
   END LOOP;
END;
/




Please do like and subscribe to my youtube channel: https://www.youtube.com/@foalabs If you like this post please follow,share and comment