According to the PL/SQL User's Guide and Reference, "A package is a schema object that groups logically related PL/SQL types, items and subprograms. " Oracle's built-in packages dramatically extend the power of the PL/SQL language. A package is far more than just a way of logically grouping objects together.
A D V E R T I S E M E N T
The main reason packages are not more widely adopted is that users are unaware of the benefits they offer.
Benefits of Oracle Packages
- Objects don't get invalidated when you makes changes to the body. That saves a lot of recompilation and makes changing the implementation much more painless. You will still have to recompile if you change the specification, but that's not something you should be doing very often.
- You can "overload" subprograms (procedures/functions). You can have several subprograms with the same name, but with a different number of parameters, or different types. That is another thing that makes implementation changes more painless because you can keep legacy code if you like. You can also see the extra flexibility that offers developers.
- You can have persistent variables throughout a session without storing anything in a database table. Packages can have variables and constants that are initialised when the packages is first used within a session, and then they are available for the remainder of the session for all future references to anything within that package. That comes in very handy.
- Speaking of initialisation, being able to call a procedure automatically the first time a package is used within a session can also come in very handy.
- You can take advantage of "encapsulation." In essence, you can hide the implementation details from users but still give them all the information they need to use the package. Since they aren't aware of the details, that means you can change them with minimal impact or risk. Packages also support private subprograms and variables which are available only to other subprograms within the package, and remain completely hidden and inaccessible to anything outside the package.
- You may notice some performance improvement when using packages. When you first use a package, the entire package may be loaded into memory, meaning fewer disk I/Os as you use the related items within.
A package is a group of procedures, functions, variables and SQL
statements created as a single unit. It is used to store together
related objects. A package has two parts, Package Specification or spec
or package header and Package Body.
Package Specification acts as an interface to the package.
Declaration of types, variables, constants, exceptions, cursors and
subprograms is done in Package specifications. Package specification
does not contain any code.
Package body is used to provide implementation for the subprograms,
queries for the cursors declared in the package specification or spec.
Advantages:
- It allows you to group together related items, types and
subprograms as a PL/SQL module.
- When a procedure in a package is called entire package is
loaded, though it happens to be expensive first time the response is
faster for subsequent calls.
- Package allows us to create types, variable and subprograms that
are private or public
|