Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - Why should I use packages instead of standalone procedures or functions

I searched google but did not find any satisfying answer as to why I should use packages.

I know that a package is a bundle of procedures, functions and different variables. As I understand it sort of corresponds to object in OOP. But of course there's nothing like instantiating different instances of a package so that each instance would have different property values and behave differently.

Then what is the advantage of using packages when I can just create a standalone procedure and call it independently?

like image 669
Mikayil Abdullayev Avatar asked Oct 17 '12 13:10

Mikayil Abdullayev


People also ask

What is the advantage of package over procedure in Oracle?

Benefits of Packages Packages are used to define related procedures, variables, and cursors and are often implemented to provide advantages in the following areas: encapsulation of related procedures and variables. declaration of public and private procedures, variables, constants, and cursors. better performance.

Why do we need packages in Oracle?

Packages prevent cascading dependencies and unnecessary recompiling. For example, if you change the body of a package function, Oracle Database does not recompile other subprograms that invoke the function, because these subprograms depend only on the parameters and return value that are declared in the specification.

What is better procedure or package?

The advantage of a package over a stand-alone procedure is that all the procedures and functions are loaded into memory so that when one procedure within the package calls another within the same package it is already loaded so this should give performance benefits if designed properly.


2 Answers

Packages provide the following advantages:

  1. Cohesion: all the procedures and functions relating to a specfic sub-system are in one program unit. This is just good design practice but it's also easier to manage, e.g. in source control.
  2. Constants, sub-types and other useful things: there's more to PL/SQL than stored procedures. Anything we can define in a package spec can be shared with other programs, for instance user-defined exceptions.
  3. Overloading: the ability to define a procedure or function with the same name but different signatures.
  4. Security: defining private procedures in the package body which can only be used by the package because they aren't exposed in the specification.
  5. Sharing common code: another benefit of private procedures.
  6. We only need to grant EXECUTE on a package rather than on several procedures.
like image 184
APC Avatar answered Sep 25 '22 01:09

APC


As described in Oracle docs, packages are good because of:

  • modularity
  • easier application design
  • information hiding
  • added functionality
  • better performance

Details on each reason are explained in docs.

like image 32
maialithar Avatar answered Sep 24 '22 01:09

maialithar