如何从程序中运行 UTL_RECOMP.RECOMP_PARALLEL?

How to run UTL_RECOMP.RECOMP_PARALLEL from a procedure?

我有一个简单的问题:是否可以从程序中 运行 UTL_RECOMP.RECOMP_PARALLEL? 我有一个包,其中包含一个应该重新编译所有无效对象的过程。它看起来像这样:

    PROCEDURE Compile ()
    IS
    BEGIN
        EXECUTE IMMEDIATE ('BEGIN SYS.UTL_RECOMP.RECOMP_PARALLEL(4,); END;');
        EXCEPTION
            WHEN OTHERS
            THEN
            DBMS_OUTPUT.PUT_LINE(SQLERRM);

    END; 

但是,我总是收到错误 PLS-00201:必须声明标识符 'UTL_RECOMP.RECOMP_PARALLEL' 我以 sys/sysdba 用户身份登录。不是这个问题。

有什么想法可以让它发挥作用吗?

谢谢!

您可以在存储过程中使用它:

SQL> set serveroutput on
SQL> --
SQL> select banner from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

SQL> show user;
USER is "SYS"
SQL> --
SQL> CREATE OR REPLACE PROCEDURE Compile
  2  IS
  3  BEGIN
  4   UTL_RECOMP.RECOMP_PARALLEL(4);
  5   EXCEPTION
  6     WHEN OTHERS
  7     THEN
  8     DBMS_OUTPUT.PUT_LINE(SQLERRM);
  9  END;
 10  /

Procedure created.

SQL> --
SQL> show errors
No errors.
SQL> --
SQL> set timing on
SQL> exec compile;

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.31
SQL> --
SQL> exit

实际上,如果该过程由 SYS 拥有并且您将 EXECUTE 权限授予另一个用户(文档说 "You must be connected AS SYSDBA to run this script"),它就可以工作。

SQL> select banner from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

SQL> show user;
USER is "SYS"
SQL> --
SQL> CREATE OR REPLACE PROCEDURE Compile
  2  IS
  3  BEGIN
  4   SYS.UTL_RECOMP.RECOMP_PARALLEL(4);
  5  END;
  6  /

Procedure created.

SQL> --
SQL> grant execute on compile to c##test;

Grant succeeded.

SQL> --
SQL> connect c##test/c##test
Connected.
SQL> show user
USER is "C##TEST"
SQL> --
SQL> drop table t purge;

Table dropped.

SQL> create table t(x int);

Table created.

SQL> create or replace procedure p
  2  is
  3  v int;
  4  begin
  5   select x into v from t;
  6  end;
  7  /

Procedure created.

SQL> --
SQL> show errors
No errors.
SQL> --
SQL> drop table t;

Table dropped.

SQL> --
SQL> select object_name, object_type, status
  2  from user_objects
  3  where object_name='P';

OBJECT_NAM OBJECT_TYP STATUS
---------- ---------- ----------
P      PROCEDURE  INVALID

SQL> --
SQL> create table t(x int);

Table created.

SQL> --
SQL> select object_name, object_type, status
  2  from user_objects
  3  where object_name='P';

OBJECT_NAM OBJECT_TYP STATUS
---------- ---------- ----------
P      PROCEDURE  INVALID

SQL> --
SQL> exec sys.compile;

PL/SQL procedure successfully completed.

SQL> --
SQL> select object_name, object_type, status
  2  from user_objects
  3  where object_name='P';

OBJECT_NAM OBJECT_TYP STATUS
---------- ---------- ----------
P      PROCEDURE  VALID

SQL> --

此类程序应 运行 仅由 SYS(如 utlrp.sql)使用 - 因此这仅适用于 DBA - 如文档所示以避免意外行为。