oracle 为什么我得到 PLS-00302:组件存在时必须声明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28706077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get PLS-00302: component must be declared when it exists?
提问by Dzyann
I am using Oracle 10.2.
我正在使用 Oracle 10.2。
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:
我正在使用一些脚本将一些 ORACLE 对象从一个 SCHEMA (S1) 移动到另一个 (S2)。我正在创建具有 DBA 角色的函数。移动时,我的一个功能无效,但我不明白为什么。它的代码是这样的:
MY_FUNC
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;
/
If I use MY_FUNC2
without the schema, It works:something := MY_FUNC2();
instead of something := S2.MY_FUNC2();
如果我在MY_FUNC2
没有架构的情况下使用,它可以工作:something := MY_FUNC2();
而不是 something := S2.MY_FUNC2();
My_FUNC2
我的_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 has a synonym like this:
MY_FUNC2 有这样的同义词:
CREATE OR REPLACE PUBLIC SYNONYM "MY_FUNC2" FOR "S2"."MY_FUNC2"
MY_FUNC
compiles with errors:
MY_FUNC
编译出错:
PLS-00302: component 'MY_FUNC2' must be declared
PLS-00302:必须声明组件“MY_FUNC2”
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.
我不明白为什么我会收到这个错误,当我的函数在另一个模式 (S1) 中时,它们具有完全相同的结构,并且同义词的创建完全相同(但指向 S1)并且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?
我最初没有创建这个函数和同义词。是否有可能我在 S2 中缺少一些权限,所以MY_FUNC
可以正常工作?
回答by Alex Poole
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
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.MY_FUNC2
正在解析的对象名称时,它不会尝试将 S2 评估为架构名称。当你只是调用它时,因为MY_FUNC2
没有混淆,所以它有效。
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.
该文档解释了名称解析。限定对象名称的第一部分 - 此处为 S2 - 在被评估为不同模式之前被评估为当前模式上的对象。
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';
回答by Tenzin
I came here because I had the same problem.
What was the problem for me was that the procedure was defined in the package body, but not in the package header.
I was executing my function with a lose BEGIN END statement.
我来这里是因为我遇到了同样的问题。
我的问题是程序是在包体中定义的,而不是在包头中。
我正在使用丢失的 BEGIN END 语句执行我的函数。