Getting error when calling any function in SQL query in a package without declaring function in package specification

怎甘沉沦 提交于 2019-12-13 08:41:15

问题


I have created a function which return NUMBER type in a package, but not declare this function in the Package Specification.

I am calling this function in SQL query in anther function with in same package body. I am getting error.

When i declare function in Package Specification Then its fine & working.

I want to know reason behind it. Please anybody explain it.


回答1:


Nothing to do with forward declaration at all.

This deals with the fact that you are using a SQL query to call the function. It seems like when using a statement to invoke a function, you are no longer inside the scope of the PL/SQL package, thus you can only call publicly available functions.

As for the why, I can only guess, so don't take it as granted, but PL/SQL and SQL have different engines. So, when doing a sql query, even inside your pl/sql package, you go to the level of SQL where it'll check again the permissions according to the SQL engine. So it has no idea it is executed from within a PL/SQL package and you should be allowed to call the private function.

I think the difference of engines can be checked easily, try to use a varchar2 of 32000, it'll work within your pl/sql function. Now, if you call your pl/sql function returning a varchar2(32000), it'll fail. Thi is a problem I ran into, but I don't have any databse to give you a snippet.




回答2:


You can use functions that are declared only in the package body, but you have to declare them before they are first used:

create or replace package pkg_so is

  function get2 return number;

end pkg_so;
/
create or replace package body pkg_so is

  function get1 return number is
  begin
    return 1;
  end;

  function get2 return number is
  begin
    return get1 + 1;
  end;

end pkg_so;
/
select pkg_so.get2 from dual
/



回答3:


yeah this is called forward declaration.If any procedure/function is not declare in package sepcificarion. Then it must be first declare in body before the first used in body.

create or replace package pkg_so is

  function get2 return number;

end pkg_so;
/
create or replace package body pkg_so is

  function get1 return number is
  begin
    return 1;
  end;

  function get2 return number is
  begin
    return get1 + 1;
  end;

end pkg_so;
/
select pkg_so.get2 from dual
/


来源:https://stackoverflow.com/questions/14725559/getting-error-when-calling-any-function-in-sql-query-in-a-package-without-declar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!