Marcello Manfredini
Marcello Manfredini

Reputation: 61

Add existing function to Package PLSQL

I am quite new to PLSQL programming so I am still facing general problems like this one.

I have developed some standalone functions (Oracle PLSQL functions) and I want to group them under the same package.

So I created a new package, but I have few questions about what to do next:

  1. Is there any possible way to link directly functions to my package? I don't think so, but maybe...
  2. Am I allowed to name functions within the package in the same way as the external functions or I will cause a scope problem?

Thank you for your help. Marcello

Upvotes: 1

Views: 3715

Answers (1)

Tony Andrews
Tony Andrews

Reputation: 132580

You can't link stand-alone function to a package, you simply have to copy the code of the function into the package body, and the function specification into the package specification.

For example if you have:

create or replace function my_function 
   return number
is
begin
   return 42;
end;

... then you could copy it into the package like this:

create or replace package my_package is
   function my_function 
      return number;
end;

and

create or replace package body my_package is
   function my_function 
      return number
   is
   begin
      return 42;
   end;
end;

You can give them the same name as the stand-alone functions without any issues. If you call one of these functions from within the package unqualified (just my_function) then you will be calling the package version; if you really wanted to call the stand-alone function from the package then you'd need to add the schema prefix: my_schmea.my_function. But hopefully you wouldn't really want to do that!

Upvotes: 4

Related Questions