为什么我得到 PLS-00302:组件存在时必须声明?

2021-12-24 00:00:00 oracle oracle10g plsql

我使用的是 Oracle 10.2.

I am using Oracle 10.2.

我正在编写一些脚本,将一些 ORACLE 对象从一个 SCHEMA (S1) 移动到另一个 (S2).我正在创建具有 DBA 角色的函数.移动时,我的一个功能无效,但我不明白为什么.它的代码是这样的:

I am working in some scripts to move some ORACLE Objects from one SCHEMA (S1) to another (S2). I am creating the functions with DBA role. When moved, one of my functions becomes invalid, but I don't understand why. Its code goes along these lines:

MY_FUNC

CREATE OR REPLACE FUNCTION S2."MY_FUNC" RETURN VARCHAR2 IS
   something VARCHAR2;
   othervar VARCHAR2 (50):= 'TEST';   
BEGIN
   something := S2.MY_FUNC2();
    /*some code*/
    return othervar;
END;
/

如果我在没有架构的情况下使用 MY_FUNC2,它可以工作:
something := MY_FUNC2(); 而不是 something := S2.MY_FUNC2();

If I use MY_FUNC2 without the schema, It works:
something := MY_FUNC2(); instead of something := S2.MY_FUNC2();

My_FUNC2

CREATE OR REPLACE FUNCTION S2."MY_FUNC2" RETURN VARCHAR2 IS
       something BOOLEAN;
       othervar VARCHAR2 (50) := 'TEST2';           
    BEGIN
       /*some code*/
        return othervar;
    END;
    /

MY_FUNC2 有这样的同义词:

MY_FUNC2 has a synonym like this:

 CREATE OR REPLACE PUBLIC SYNONYM "MY_FUNC2" FOR "S2"."MY_FUNC2"

MY_FUNC 编译出错:

PLS-00302:必须声明组件MY_FUNC2"

PLS-00302: component 'MY_FUNC2' must be declared

我不明白为什么我会收到这个错误,当我的函数在另一个模式 (S1) 中时,它们具有完全相同的结构,并且同义词的创建完全相同(但指向 S1)和 MY_FUNC 编译良好.

I don't understand why I am getting this error, when my functions were in the other schema (S1) they had exactly the same structure and the synonym was created exactly the same (but pointing to S1) and MY_FUNC compiled fine.

我最初没有创建这个函数和同义词.是不是我在 S2 中缺少一些权限,所以 MY_FUNC 可以正常工作?

I didn't create this functions and synonym originally. Is it possible that I am missing some privileges in S2, so MY_FUNC can work properly?

推荐答案

如果您有一个与架构同名的对象,就会出现该错误.例如:

You can get that error if you have an object with the same name as the schema. For example:

create sequence s2;

begin
  s2.a;
end;
/

ORA-06550: line 2, column 6:
PLS-00302: component 'A' must be declared
ORA-06550: line 2, column 3:
PL/SQL: Statement ignored

当您引用 S2.MY_FUNC2 时,对象名称正在解析,因此它不会尝试将 S2 评估为架构名称.当您将其称为 MY_FUNC2 时,没有任何混淆,因此它可以工作.

When you refer to S2.MY_FUNC2 the object name is being resolved so it doesn't try to evaluate S2 as a schema name. When you just call it as MY_FUNC2 there is no confusion, so it works.

文档解释了名称解析.限定对象名称的第一部分 - 此处为 S2 - 在被评估为不同模式之前被评估为当前模式上的对象.

The documentation explains name resolution. The first piece of the qualified object name - S2 here - is evaluated as an object on the current schema before it is evaluated as a different schema.

它可能不是一个序列;其他对象可能会导致相同的错误.您可以通过查询数据字典来检查是否存在同名对象.

It might not be a sequence; other objects can cause the same error. You can check for the existence of objects with the same name by querying the data dictionary.

select owner, object_type, object_name
from all_objects
where object_name = 'S2';

相关文章