如何使用私有过程创建 Oracle 包?
How to create an Oracle package with private procedure?
我正在创建一个 Oracle 程序包,我想知道是否可以将程序设为私有,这是我的示例代码:
CREATE OR REPLACE PACKAGE MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2);
PROCEDURE MyProcedureB(inputParam IN VARCHAR2);
END MyPackage;
/
CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2) AS
myHello VARCHAR2(1000) := 'Hello';
BEGIN
MyProcedureB(myHello);
outputParam := 'OK';
END MyProcedureA;
PROCEDURE MyProcedureB(inputParam IN VARCHAR2) AS
myWorld VARCHAR2(1000) := 'World';
BEGIN
dbms_output.put_line(inputParam || myWorld);
END MyProcedureB;
END MyPackage;
是否可以制作 MyProcedureB 'private',我的意思是它只能被包 (MyProcedureA) 中的其他程序调用?
当然可以。包规范中未声明的任何内容都是私有的。
但是,一旦您从包规范中删除 MyProcedureB
,您就会在 MyProcedureA
中调用它之前对其进行声明。我通常更喜欢通过将 MyProcedureB
的实现放在包体中 MyProcedureA
的实现之前来做到这一点。
CREATE OR REPLACE PACKAGE MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2);
END MyPackage;
/
CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureB(inputParam IN VARCHAR2) AS
myWorld VARCHAR2(1000) := 'World';
BEGIN
dbms_output.put_line(inputParam || myWorld);
END MyProcedureB;
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2) AS
myHello VARCHAR2(1000) := 'Hello';
BEGIN
MyProcedureB(myHello);
outputParam := 'OK';
END MyProcedureA;
END MyPackage;
您也可以保留现有订单,只需在包体顶部声明程序规范
CREATE OR REPLACE PACKAGE MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2);
END MyPackage;
/
CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureB(inputParam IN VARCHAR2);
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2) AS
myHello VARCHAR2(1000) := 'Hello';
BEGIN
MyProcedureB(myHello);
outputParam := 'OK';
END MyProcedureA;
PROCEDURE MyProcedureB(inputParam IN VARCHAR2) AS
myWorld VARCHAR2(1000) := 'World';
BEGIN
dbms_output.put_line(inputParam || myWorld);
END MyProcedureB;
END MyPackage;
我正在创建一个 Oracle 程序包,我想知道是否可以将程序设为私有,这是我的示例代码:
CREATE OR REPLACE PACKAGE MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2);
PROCEDURE MyProcedureB(inputParam IN VARCHAR2);
END MyPackage;
/
CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2) AS
myHello VARCHAR2(1000) := 'Hello';
BEGIN
MyProcedureB(myHello);
outputParam := 'OK';
END MyProcedureA;
PROCEDURE MyProcedureB(inputParam IN VARCHAR2) AS
myWorld VARCHAR2(1000) := 'World';
BEGIN
dbms_output.put_line(inputParam || myWorld);
END MyProcedureB;
END MyPackage;
是否可以制作 MyProcedureB 'private',我的意思是它只能被包 (MyProcedureA) 中的其他程序调用?
当然可以。包规范中未声明的任何内容都是私有的。
但是,一旦您从包规范中删除 MyProcedureB
,您就会在 MyProcedureA
中调用它之前对其进行声明。我通常更喜欢通过将 MyProcedureB
的实现放在包体中 MyProcedureA
的实现之前来做到这一点。
CREATE OR REPLACE PACKAGE MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2);
END MyPackage;
/
CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureB(inputParam IN VARCHAR2) AS
myWorld VARCHAR2(1000) := 'World';
BEGIN
dbms_output.put_line(inputParam || myWorld);
END MyProcedureB;
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2) AS
myHello VARCHAR2(1000) := 'Hello';
BEGIN
MyProcedureB(myHello);
outputParam := 'OK';
END MyProcedureA;
END MyPackage;
您也可以保留现有订单,只需在包体顶部声明程序规范
CREATE OR REPLACE PACKAGE MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2);
END MyPackage;
/
CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MyPackage AS
PROCEDURE MyProcedureB(inputParam IN VARCHAR2);
PROCEDURE MyProcedureA(outputParam OUT VARCHAR2) AS
myHello VARCHAR2(1000) := 'Hello';
BEGIN
MyProcedureB(myHello);
outputParam := 'OK';
END MyProcedureA;
PROCEDURE MyProcedureB(inputParam IN VARCHAR2) AS
myWorld VARCHAR2(1000) := 'World';
BEGIN
dbms_output.put_line(inputParam || myWorld);
END MyProcedureB;
END MyPackage;