Package - pl/sql programming, PL-SQL Programming

Assignment Help:

What Is a Package?

The package is a schema object that group logically related PL/SQL items, types, and subprograms. The Packages usually have 2 parts, a specification & a body, though many times the body is needless. The specification is the interface to your applications; it declares the type, constants, variables, exceptions, cursors, and subprograms accessible for use. The body fully defines the cursors & subprograms, and so equipment the specification.

The figure shows, the specification as an operational interface and of the body as the "black box." You can enhance, debug, or replace the package body without changing the interface to the package.

1715_package.png

Figure: Package Interface

To build packages, use the CREATE PACKAGE statement that you can execute interactively from the SQL Plus. The syntax for the same is as shown:

CREATE [OR REPLACE] PACKAGE package_name

[AUTHID {CURRENT_USER | DEFINER}] {IS | AS}

[type_definition [type_definition] ...]

[cursor_spec [cursor_spec] ...]

[item_declaration [item_declaration] ...]

[{subprogram_spec | call_spec} [{subprogram_spec | call_spec}]...]

END [package_name];

[CREATE [OR REPLACE] PACKAGE BODY package_name {IS | AS}

[type_definition [type_definition] ...]

[cursor_body [cursor_body] ...]

[item_declaration [item_declaration] ...]

[{subprogram_spec | call_spec} [{subprogram_spec | call_spec}]...]

[BEGIN

sequence_of_statements]

END [package_name];]

The specifications hold the public declarations that are visible to your application. The body holds the implementation details and private declarations that are hidden from your application. The declarative section below of the package body is the optional initialization section that typically holds the statements that initialize the package variables. The AUTHID clause determine whether all the packaged subprograms execute with the privileges of their definer or invoker, and whether their unqualified references to schema objects are solved in the schema of the definer or invoker.

The call specification publishes a Java method or external C function in the Oracle data dictionary. The call specification publishes the routine by mapping its parameter types, name, and return type to their SQL counterparts.

In the illustration below, you package a cursor, a record type, and two employment procedures. Note that the procedure hire_employee uses the database series empno_seq and the function SYSDATE to insert a new employee number & hire date, correspondingly.

CREATE OR REPLACE PACKAGE emp_actions AS -- spec

TYPE EmpRecTyp IS RECORD (emp_id INTEGER, salary REAL);

CURSOR desc_salary RETURN EmpRecTyp;

PROCEDURE hire_employee (

ename VARCHAR2,

job VARCHAR2,

mgr NUMBER,

sal NUMBER,

comm NUMBER,

deptno NUMBER);

PROCEDURE fire_employee (emp_id NUMBER);

END emp_actions;

CREATE OR REPLACE PACKAGE BODY emp_actions AS -- body

CURSOR desc_salary RETURN EmpRecTyp IS

SELECT empno, sal FROM emp ORDER BY sal DESC;

PROCEDURE hire_employee (

ename VARCHAR2,

job VARCHAR2,

mgr NUMBER,

sal NUMBER,

comm NUMBER,

deptno NUMBER) IS

BEGIN

INSERT INTO emp VALUES (empno_seq.NEXTVAL, ename, job,

mgr, SYSDATE, sal, comm, deptno);

END hire_employee;

PROCEDURE fire_employee (emp_id NUMBER) IS

BEGIN

DELETE FROM emp WHERE empno = emp_id;

END fire_employee;

END emp_actions;

The declarations in the package specification are only visible & accessible to the applications.

The Implementation details in the package body are hidden and inaccessible. Therefore, you can change the body without having to recompile the calling programs.


Related Discussions:- Package - pl/sql programming

Adding table constraints, Adding Table Constraints ALTER TABLE ENROL...

Adding Table Constraints ALTER TABLE ENROLMENT ADD CONSTRAINT NameNotNull CHECK (Name IS NOT NULL) ; ALTER TABLE ENROLMENT ADD CONSTRAINT PK_StudentId_CourseId PRIM

Effects of null in table literal, Effects of NULL in Table Literal Whe...

Effects of NULL in Table Literal When a VALUES expression appears as the source value for an SQL INSERT statement, the key word NULL can appear as a field value, such that for

Object types and collections - performance of application, Use Object Types...

Use Object Types and Collections The Collection types and object types increase your efficiency by allowing for the realistic data modeling. The Complex real-world entities an

Origin of earth - big bang hypothesis, ORIGIN OF EARTH - BIG-BAN G HY...

ORIGIN OF EARTH - BIG-BAN G HYPOTHESIS - Origin of life is linked to origin of earth. Cosmos, the Universe originated 10-20 billion years ago by Big Bang (thermonu

Quantification in sql, Quantification in SQL To quantify something, as...

Quantification in SQL To quantify something, as the theory book has it, is to state its quantity, to say how many of it there are. For example, in Tutorial D the expression CO

Avoid the not null constraint - performance of application, Avoid the NOT N...

Avoid the NOT NULL Constraint In the PL/SQL, using the NOT NULL constraint incur a performance cost. Consider the illustration as shown below: PROCEDURE calc_m IS m NUMB

Advantages of exceptions, Advantages of Exceptions Using the exception...

Advantages of Exceptions Using the exceptions for the error handling has many benefits. Without an exception handling, every time you issue a command, you should ensure for th

Parameter aliasing, Parameter Aliasing   To optimize the subprogram ca...

Parameter Aliasing   To optimize the subprogram call, the PL/SQL compiler can decide between the two techniques of the parameter passing. With the by-value techniques, the v

I want online credit application website, Project Description: We organi...

Project Description: We organize an online system called ACPAS we have created a project called EVO that can be use by our customers to integrate their web sites with the Acpas

Defining and declaring collections, Defining and Declaring Collections T...

Defining and Declaring Collections To create the collections, you must define a collection type, and then declare the collections of that type. You can define the VARRAY types a

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd