Home >Database >Mysql Tutorial >用DBMS_DDL包重新编译PL/SQL对象

用DBMS_DDL包重新编译PL/SQL对象

WBOY
WBOYOriginal
2016-06-07 15:15:591379browse

ALTER_COMPILE过程 这个过程等价于如下语句 ALTER PROCEDURE|FUNCTION|PACKAGE [ .] COMPILE [BODY] 语法 DBMS_DDL.ALTER_COMPILE ( type VARCHAR2, schema VARCHAR2, name VARCHAR2); 参数: 现在,如果想重 编译 一个程序,Oracle首先会重 编译 被标识invali

ALTER_COMPILE过程

这个过程等价于如下语句

ALTER PROCEDURE|FUNCTION|PACKAGE [.] COMPILE [BODY]

语法

DBMS_DDL.ALTER_COMPILE (

type

VARCHAR2,

schema VARCHAR2,

name

VARCHAR2);

参数:

用DBMS_DDL包重新编译PL/SQL对象


现在,如果想重编译一个程序,Oracle首先会重编译被标识invalid的程序所依赖的所有对象.

例1:

编译MYUSER方案的ACCESS_RIGHTS过程:

DBMS_DDL.ALTER_COMPILE('PROCEDURE','MYUSER','ACCESS_RIGHTS');

例2:

差不多每个实例中,PL/SQL对象名称以大写方式存储,如果创建时指定一个双引号的对象名称,那么当编译时应指定同样的方式.

如果指定不正确,则会报以下的错误

SQL> dbms_ddl.alter_compile('PROCEDURE','MYUSER','access_rights');

BEGIN dbms_ddl.alter_compile('PROCEDURE','MYUSER','access_rights'); END;

*

ERROR at line 1:

ORA-20000: Unable to compile PROCEDURE "MYUSER"."access_rights",

insufficient privileges or does not exist

ORA-06512: at "SYS.DBMS_DDL", line 68

ORA-06512: at line 1

因此如果你创建如下的对象:

CREATE or REPLACE PROCEDURE "My_Object" IS

BEGIN

...

得使用:

DBMS_DDL.ALTER_COMPILE('PROCEDURE','MYUSER','My_Object');

也可以使用ALTER PROCEDURE COMPILE命令来编译对象,用DBMS_DDL包最大的优势在于可以在以后的程序或PL/SQL中编译,这将拥有完全的灵活性以创建强有力的使用价值(请参照如下例子)

例3:

这个例子将获得所有无效的对象并且重新编译

set termout on

set serverout on

DECLARE

cursor cur_invalid_objects is

select object_name, object_type from user_objects where status='INVALID';

rec_columns cur_invalid_objects%ROWTYPE;

err_status NUMERIC;

BEGIN

dbms_output.enable(10000);

open cur_invalid_objects;

loop

fetch cur_invalid_objects into rec_columns;

EXIT WHEN cur_invalid_objects%NOTFOUND;

dbms_output.put_line

('Recompiling ' || rec_columns.object_type || '

' || rec_columns.object_name);

dbms_ddl.alter_compile(rec_columns.object_type,NULL,rec_columns.object_name);

end loop;

close cur_invalid_objects;

EXCEPTION

When others then

begin

err_status := SQLCODE;

dbms_output.put_line(' Recompilation failed : ' || SQLERRM(err_status));

if ( cur_invalid_objects%ISOPEN) then

CLOSE cur_invalid_objects;

end if;

exception when others then

null;

end;

end;

/

总结:

ALTER_COMPILE可以代替手工而自动编译对象,查找那些无效的对象从而一个一个地编译.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn