The package specification is required when you create a new package. The package specification lists all the objects which are publicly accessible from other applications. The package specification also provides the information that developers need to know in order to use the interface.
These packages may contain only type definition and variables declaration. Those variables are known as package data. If a package specification whose public objects include cursors and subprograms, then it must have a body which defines queries for the cursors and code for the subprograms. A package body contains the implementation of the cursors or subprograms declared in the package specification. In the package body, you can declare or define private variables, cursors, etc.
A package body can have an initialization part whose statements initialize variables or perform other one-time setups for the whole package. A package body can also have an exception-handling part used to handle exceptions. The following are the advantages of the package:.
By doing this, you make each package more reusable, manageable, readable and reliable. In some applications, such as Oracle Office, a typical session lasts several days. The package state persists only for the life of a server call. After the server call, the work area returns to the pool. If a subsequent server call references the package, then Oracle Database reuses an instantiation from the pool. Reusing an instantiation re-initializes it; therefore, changes made to the package state in previous server calls are invisible.
For information about initialization, see " Package Instantiation and Initialization ". You must use its public variables only within the work unit. If you make a mistake and depend on the value of a public variable that was set in a previous work unit, then your program can fail. Each package declares public variable n with initial value 5. Then, an anonymous block changes the value of each variable to Next, another anonymous block prints the value of each variable.
The value of pkg. To re-open the cursor, you must make a new server call. A server call can be different from a subprogram invocation, as Example shows. Become familiar with the packages that Oracle Database supplies, and avoid writing packages that duplicate their features. This practice prevents other developers from building unsafe dependencies on your implementation details and reduces the need for recompilation. If you change the package specification, you must recompile any subprograms that invoke the public subprograms of the package.
If you change only the package body, you need not recompile those subprograms. Declare public cursors in package specifications and define them in package bodies, as in Example This practice lets you hide cursors' queries from package users and change them without changing cursor declarations. If computing an initial value raises an exception, the initialization part can handle it with its own exception handler.
The cursor declaration specifies only the data type of the return value, not the query, which appears in the cursor definition for complete syntax and semantics, see " Explicit Cursor Declaration and Definition ". Example creates an API package and a helper package. The package has both specification and body. The specification declares a public type, cursor, and exception, and three public subprograms. One public subprogram is overloaded for information about overloaded subprograms, see " Overloaded Subprograms ".
The body declares a private variable, defines the public cursor and subprograms that the specification declares, declares and defines a private function, and has an initialization part.
You need not qualify references to its contents by prefixing the package name. For example, you might invoke ABS from a database trigger, stored subprogram, Oracle tool, or 3GL application, as follows:. If you declare your own version of ABS , your local declaration overrides the public declaration.
You can still invoke the SQL function by specifying its full name:. Most SQL functions are overloaded. Easier Application Design When designing an application, all you need initially is the interface information in the package specifications. Hidden Implementation Details Packages let you share your interface information in the package specification, and hide the implementation details in the package body.
Hiding the implementation details in the body has these advantages: You can change the implementation details without affecting the application interface. Added Functionality Package public variables and cursors can persist for the life of a session. Better Performance The first time you invoke a package subprogram, Oracle Database loads the whole package into memory. Easier to Grant Roles You can grant roles on the package, instead of granting roles on each object in the package.
Note: You cannot reference host variables from inside a package. Package Specification A package specification declares public items. Note: You cannot reference remote package public variables, even indirectly. Package Body If a package specification declares cursors or subprograms, then a package body is required; otherwise, it is optional.
Package Instantiation and Initialization When a session references a package item, Oracle Database instantiates the package for that session. Initialization includes whichever of the following are applicable: Assigning initial values to public constants Assigning initial values to public variables whose declarations specify them Executing the initialization part of the package body.
0コメント