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

Name resolution - pl/sql, Name Resolution   During the compilation, th...

Name Resolution   During the compilation, the PL/SQL compiler relates identifiers like the name of a variable with an address or memory location, actual value, or datatype. Th

Write sql queries, Write SQL queries to solve the following specifications....

Write SQL queries to solve the following specifications. Include the query AND THE OUTPUT.  A screen dump of the output is acceptable. Show as many rows as you can. A screen dump i

Package standard, Package STANDARD The package named STANDARD defines t...

Package STANDARD The package named STANDARD defines the PL/SQL atmosphere. The package specification globally declares the exceptions, types, and subprograms that are available

Usefulness of data type, Usefulness of Data Type In SQL, as in most c...

Usefulness of Data Type In SQL, as in most computer languages, a type can be used for constraining the values that are permitted to be used for some purpose. In particular, i

Introduction to oracle, Introduction Oracle 9i - it was made publ...

Introduction Oracle 9i - it was made public in the year 2001 with over 400 features, and graphics, it has merged the traditional business with modern internet application

Union all - sql, UNION ALL - SQL Further varieties of UNION arise when...

UNION ALL - SQL Further varieties of UNION arise when we replace the key word DISTINCT by ALL in any of the foregoing examples, as in Example. ALL specifies that if row r appe

Relational algebra, Define basic operators of relational algebra with an ex...

Define basic operators of relational algebra with an example each

Homework, What are the rates for help in writing PL/SQL procedures and func...

What are the rates for help in writing PL/SQL procedures and functions?

Character types, ROWID and UROWID Internally, every database table has ...

ROWID and UROWID Internally, every database table has a ROWID pseudo column that stores binary values known as rowids. Each rowid shows the storage address of a row. A physical

Implicit cursors, Implicit Cursors The Oracle implicitly opens a curso...

Implicit Cursors The Oracle implicitly opens a cursor to process each SQL statement not related with an explicitly declared cursor. The PL/SQL lets you refer to the most recen

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