在包体内调用私有函数 [英] Calling private function within package body

查看:97
本文介绍了在包体内调用私有函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Oracle文档,可以通过在主体中而不是在规范中声明项目来使项目在包中私有.

According to Oracle documentation one can make items private in packages by declaring them in the body but not in the specification.

此程序包中有一个过程,需要调用一个不应在此程序包外部访问的函数. Oracle SQL Developer返回PLS-00313 'ADD_STUDENT' not declared in this scope

I have a procedure in this package that needs to call a function that should not be accessed outside of this package. Oracle SQL Developer returns PLS-00313 'ADD_STUDENT' not declared in this scope

声明:

PACKAGE SCHOOL AS
    PROCEDURE ADD_PEOPLE(...);
END SCHOOL;

身体:

PACKAGE BODY SCHOOL AS
    PROCEDURE ADD_PEOPLE(...)
        ...
        ADD_STUDENT();
    END ADD_PEOPLE;

    FUNCTION ADD_STUDENT(...)
        ...
    END ADD_STUDENT;
END SCHOOL;

我找不到调用内部函数/过程以及是否需要包声明的示例,例如:SCHOOL.ADD_STUDENT()

I can't find an example of calling internal functions/procedures and whether the package declaration is needed ex:SCHOOL.ADD_STUDENT()

推荐答案

您遇到的问题(假设您以正确的方式调用了正确命名的过程/函数)是您尝试调用对函数的调用尚未被宣布.假设您想使函数保持私有状态,有两种解决方法:

The issue you have (assuming that you are calling the correctly named procedure/function in the correct manner) is that you are trying to invoke a call to a function that hasn't yet been declared. There are two ways around this, assuming you want to keep the function private:

  1. 在调用它的任何过程/函数之前声明ADD_STUDENT函数.
  2. 转发声明之前声明函数它被调用.
  1. Declare the ADD_STUDENT function before any procedures/functions that invoke it.
  2. Use forward declaration to declare the function before it is invoked.

因此,对于选项1,示例代码如下:

So, for option 1, your example code would look like:

PACKAGE BODY SCHOOL AS
    FUNCTION ADD_STUDENT(...)
        ...
    END ADD_STUDENT;

    PROCEDURE ADD_PEOPLE(...)
        ...
        some_var := ADD_STUDENT();
    END ADD_PEOPLE;
END SCHOOL;
/

对于选项2,您的代码如下所示:

And for option 2 your code would look like:

PACKAGE BODY SCHOOL AS
    -- forward declared function
    FUNCTION ADD_STUDENT(...);

    PROCEDURE ADD_PEOPLE(...)
        ...
        some_var := ADD_STUDENT();
    END ADD_PEOPLE;

    FUNCTION ADD_STUDENT(...)
        ...
    END ADD_STUDENT;
END SCHOOL;
/

我个人比较喜欢选项1,因为这意味着更少的东西堆满了程序包主体,但是如果您有两个相互引用的模块,则选项2可能是必需的.

Personally, I favour option 1, as it means there's less stuff cluttering up the package body, but option 2 might be necessary if you have two modules that reference each other.

这篇关于在包体内调用私有函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆