编译oracle invalid_objects .....想知道这是否正确

时间:2011-08-23 11:46:02

标签: oracle object compilation

 /* Formatted on 23/08/2011 12:42:42 (QP5 v5.163.1008.3004) */ 
 CREATE OR REPLACE PROCEDURE COUNT_N_COMPILE AS    

 ERR_MSG        VARCHAR2(100);    
 COUNT_ERRORS   NUMBER := 0;    
 A              NUMBER := 0;  
 B              NUMBER := 0;   
 C              NUMBER := 0;    
 D              NUMBER := 0;

    CURSOR CUR_LOOP    IS
       SELECT OWNER, OBJECT_NAME, OBJECT_TYPE
         FROM DBA_OBJECTS
        WHERE OBJECT_TYPE IN
                 ('PACKAGE',
                  'PACKAGE BODY',
                  'VIEW',
                  'INDEX',
                  'PROCEDURE',
                  'FUNCTION',
                  'MATERIALIZED VIEW',
                  'SYNONYM')
              AND OBJECT_NAME NOT IN ('%$%')
              AND STATUS != 'VALID'; 
BEGIN   
 <GENERIC_CODE.DROPIFEXISTS (USER, 'COUNT_N_COMPILE_TABLE'); --drops if the table exists

    EXECUTE IMMEDIATE 'CREATE TABLE COUNT_N_COMPILE_TABLE
                     (
                     OWNER VARCHAR2(300),
                     OBJECT_NAME VARCHAR2(300),
                     ERROR_STORED VARCHAR2(3000)
                     )
                     LOGGING 
                     NOCOMPRESS 
                     NOCACHE
                     NOPARALLEL
                     MONITORING';

    FOR CUR_REC IN CUR_LOOP    LOOP
       BEGIN
          CASE
             WHEN CUR_REC.OBJECT_TYPE IN
                     ('VIEW', 'INDEX', 'PROCEDURE', 'FUNCTION')
             THEN
                A := A + 1;

                EXECUTE IMMEDIATE
                      'ALTER '
                   || CUR_REC.OBJECT_TYPE
                   || ' "'
                   || CUR_REC.OWNER
                   || '"."'
                   || CUR_REC.OBJECT_NAME
                   || '" COMPILE';
             WHEN CUR_REC.OBJECT_TYPE = 'MATERIALIZED VIEW'
             THEN
                B := B + 1;

                EXECUTE IMMEDIATE
                      'ALTER '
                   || CUR_REC.OBJECT_TYPE
                   || ' "'
                   || CUR_REC.OWNER
                   || '"."'
                   || CUR_REC.OBJECT_NAME
                   || '" COMPILE';
             WHEN CUR_REC.OBJECT_TYPE = 'SYNONYM'
             THEN
                C := C + 1;

                EXECUTE IMMEDIATE
                      'ALTER '
                   || CUR_REC.OBJECT_TYPE
                   || ' "'
                   || CUR_REC.OWNER
                   || '"."'
                   || CUR_REC.OBJECT_NAME
                   || '" COMPILE';
             WHEN CUR_REC.OBJECT_TYPE = 'PACKAGE'
             THEN
                D := D + 1;

                EXECUTE IMMEDIATE
                      'ALTER '
                   || CUR_REC.OBJECT_TYPE
                   || ' "'
                   || CUR_REC.OWNER
                   || '"."'
                   || CUR_REC.OBJECT_NAME
                   || '" COMPILE';
             ELSE
                D := D + 1;

                EXECUTE IMMEDIATE
                      'ALTER PACKAGE "'
                   || CUR_REC.OWNER
                   || '"."'
                   || CUR_REC.OBJECT_NAME
                   || '" COMPILE BODY';
          END CASE;
       EXCEPTION
          WHEN OTHERS
          THEN
             ERR_MSG := SUBSTR (CUR_REC.OBJECT_TYPE || '  ' || SQLERRM, 1, 100);
             DBMS_OUTPUT.PUT_LINE (ERR_MSG);
             COUNT_ERRORS := COUNT_ERRORS + 1;

             INSERT
               INTO COUNT_N_COMPILE_TABLE (OWNER, OBJECT_NAME, ERROR_STORED)
             VALUES (CUR_REC.OWNER, CUR_REC.OBJECT_NAME, ERR_MSG);
       END;    
       END LOOP;

    DBMS_OUTPUT.PUT_LINE ('PROC, FUNCTION, INDEX, VIEWS   ' || A);   
    DBMS_OUTPUT.PUT_LINE ('PACKAGES    ' || D);    
    DBMS_OUTPUT.PUT_LINE ('MATERIALIZED VIEW    ' || B);    
    DBMS_OUTPUT.PUT_LINE ('SYNONYMS   ' || C);  
    DBMS_OUTPUT.PUT_LINE (' TOTAL NUMBER OF ERRORS    ' || COUNT_ERRORS);    
     COMMIT; 
END; 

/

1 个答案:

答案 0 :(得分:4)

你可以使用:

exec dbms_utility.compile_schema( 'YOURSCHEMAHERE' )

另见:http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:637156098168