oracle 包装规格中的程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12707204/
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
Procedure in package specification
提问by J?cob
I have a package named save_db_values
我有一个名为save_db_values的包
I have two procedures named store_recordsand another one called db_activities. db_activitieswill be called from my application by passing all values in db_activitiesI will be calling store_recordsprocedure to do insert and delete.
我有两个名为store_records 的过程和另一个名为db_activities 的过程。 db_activities将通过传递db_activities 中的所有值从我的应用程序调用我将调用store_records过程来执行插入和删除。
Do I need to define store_recordsprocedure in package specification? When I did not define store_records in specification I am getting error store_records not declared in this scope.
我需要在包规范中定义store_records过程吗?当我没有在规范中定义 store_records 时,我收到错误store_records not declared in this scope.
store_recordsprocedure I do not want to expose and hence I did not add in specification. How can I resolve this issue?
store_records程序我不想公开,因此我没有添加规范。我该如何解决这个问题?
回答by Nick Krasnov
If you do not want some procedures to be publicly available you may not to declare them in the package specification. Declare them only in the package body. The cause of the error you are facing is declaration order of the procedures in the package body or lack of forward declaration. For example:
如果您不希望某些过程公开可用,则可能不会在包规范中声明它们。仅在包体中声明它们。您遇到的错误的原因是包体中程序的声明顺序或缺少前向声明。例如:
create or replace package Test_pkg as
2 procedure Proc1;
3 end;
4 /
Package created
create or replace package body Test_pkg as
2
3 procedure proc1 is
4 begin
5 proc2;
6 end;
7
8 procedure Proc2 is
9 begin
10 dbms_output.put_line('proc2 is being executed');
11 end;
12
13 end;
14 /
Warning: Package body created with compilation errors
Error: PLS-00313: 'PROC2' not declared in this scope
This is happening because we are calling Proc2
which declared later in the package. In this case our choices are:
发生这种情况是因为我们正在调用Proc2
稍后在包中声明的 which。在这种情况下,我们的选择是:
Declare pro2
before the procedure which calls it
pro2
在调用它的过程之前声明
create or replace package body Test_pkg as
2
3
4 procedure Proc2 is
5 begin
6 dbms_output.put_line('proc2 is being executed');
7 end;
8
9 procedure proc1 is
10 begin
11 proc2;
12 end;
13
14 end;
15 /
Package body created
Use forward declaration.
使用前向声明。
create or replace package body Test_pkg as
2
3 procedure Proc2;
4
5 procedure proc1 is
6 begin
7 proc2;
8 end;
9
10 procedure Proc2 is
11 begin
12 dbms_output.put_line('proc2 is being executed');
13 end;
14
15
16 end;
17 /
Package body created
SQL> exec test_pkg.Proc1;
proc2 is being executed
PL/SQL procedure successfully completed
回答by Erich Kitzmueller
You can declare procedures just in the body, but the order in which they appear is relevant; the calling procedure must be defined after the called procedure. Or you use a forward declaration to make it easier:
您可以只在正文中声明过程,但它们出现的顺序是相关的;调用过程必须在被调用过程之后定义。或者您使用前向声明使其更容易:
package save_db_values is
procedure db_activities;
end save_db_values;
package body save_db_values is
procedure store records; -- forward declaration
procedure db_activities is
begin
store_records;
end;
procedure store records is
begin
null;
end;
end save_db_values;
回答by Ritesh Jain Fujitsu Consulting
It is happening because of writing procedure's body in the package body . if you are not declaring any procedure in the package specification then you should write it at first place.
这是因为在包 body 中写入程序的 body 。如果您没有在包规范中声明任何过程,那么您应该首先编写它。
it will work :)
它会起作用:)