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

Pl/sql assignment, 1. a. Write a trigger that fires when a part's price...

1. a. Write a trigger that fires when a part's price is updated. The trigger will write a record into a table called PriceUpdates. The record should contain the information of

Solve the business problems using sql, Use the MASCOT tables CREDITRS, PORD...

Use the MASCOT tables CREDITRS, PORDS and PAYMENTS to write SQL queries to solve the following business problems. These tables / data are available to you via the USQ Oracle server

Packages, Packages The package is a schema object which groups logicall...

Packages The package is a schema object which groups logically associated to the PL/SQL items, types, and subprograms. The Packages have 2 sections: the specification & the bod

Using %type-declarations in sql, Using %TYPE The %TYPE attribute gives th...

Using %TYPE The %TYPE attribute gives the datatype of a variable or the database column. In the example below, the %TYPE gives the datatype of a variable: credit REAL(7,2); debi

Built-in functions-comparison operators, Built-In Functions The PL/SQL p...

Built-In Functions The PL/SQL provides a lot of powerful functions to help you to manipulate the data. These built-in functions fall into the categories as shown below: error r

Build a purchases report that matches the general ledger, Great Plains (Mic...

Great Plains (Microsoft Dynamics) Purchases Report Project Description: I want to build a purchases report that matches the General Ledger. presently, when I join the PM20

Albeit simple method , Albeit simple method : These all the truth tabl...

Albeit simple method : These all the truth tables give us our first as albeit simple method for proving a theorem: where check whether it can be written in propositional logic

Positional and named notation, Positional and Named Notation You can wr...

Positional and Named Notation You can write the actual parameters when calling a subprogram, using either positional or named notation. That is, you can point to the relationsh

Cursors, What is Cursors how to use it in Real time application ?

What is Cursors how to use it in Real time application ?

Need to change mysql query to pdo, Need to change mysql query to PDO Pro...

Need to change mysql query to PDO Project Description: I want someone to convert me 1 .php file that includes some sql/mysql stuff to PDO is a very small file. Skills requ

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