You are on page 1of 8

CHAPTER Packages

18

This chapter describes Packages under PL/SQL: PL/SQL packages are schema objects that groups logically related PL/SQL types, variables and subprograms. A package will mandatory parts Package speci!ication Package de!inition body or have two

Package Specification "he speci!ication is the inter!ace to the package. #t just $%&LA'%S the types, variables, constants, e(ceptions, cursors, and subprograms that can be re!erenced !rom outside the package. #n other words, it contains all in!ormation about the content o! the package, but e(cludes the code !or the subprograms. All objects placed in the speci!ication are called public objects. Any subprogram not in the package speci!ication but coded in the package body is called a private object. "he !ollowing code snippet shows a package speci!ication having a single procedure. )ou can have many global variables de!ined and multiple procedures or !unctions inside a package. CREATE PACKAGE cust_sal AS PROCEDURE find_sal(c_id customers.id%t !e"# E$D cust_sal# % *hen the above code is e(ecuted at SQL prompt, it produces !ollowing result Pac&a'e created.

TUTORIALSPOINT Simply Easy Learning

Page

92

Package Body "he package body has the codes !or various methods declared in the package speci!ication and other private declarations, which are hidden !rom code outside the package. "he &'%A"% PA&+A,% -.$) Statement is used !or creating the package body. "he !ollowing code snippet shows the package body declaration !or the cust_sal package created above. # assumed that we already have &/S".0%'S table created in our database as mentioned in PL/SQL 1 2ariableschapter. CREATE OR REP(ACE PACKAGE )OD* cust_sal AS PROCEDURE find_sal(c_id customers.id%T*PE" +S c_sal customers.salar %T*PE# )EG+$ SE(ECT salar +$TO c_sal ,RO- customers ./ERE id 0 c_id# d1ms_out!ut.!ut_line(2Salar 3 244 c_sal"# E$D find_sal# E$D cust_sal# % *hen the above code is e(ecuted at SQL prompt, it produces !ollowing result Pac&a'e 1od created.

Using the Package Elements "he package elements 3 variables, procedures or !unctions4 are accessed with the !ollowing synta( !ac&a'e_name.element_name# &onsider, we already have created above package in our database schema, the !ollowing program uses the !ind5sal method o! the cust5sal package DEC(ARE code customers.id%t !e 30 5cc_id# )EG+$ cust_sal.find_sal( code"# E$D# % *hen the above code is e(ecuted at SQL prompt, it prompt to enter customer #$ and when you enter an #$, it displays corresponding salary

as !ollows Enter 6alue for cc_id3 7 Salar 3 8999 P(%S:( !rocedure successfull Example com!leted.

TUTORIALSPOINT Simply Easy Learning 93

Page

"he !ollowing program provides a more complete package. *e will use the &/S".0%'S table stored in our database with the !ollowing records Select ; from customers# <====<==========<=====<===========<==========< 4 +D 4 $A-E 4 AGE 4 ADDRESS 4 SA(AR* 4 <====<==========<=====<===========<==========< 4 7 4 Rames> 4 8? 4 A>meda1ad 48999.99 4 ? 4 K>ilan 4 ?@ 4 Del>i 4 8999.99 4 8 4 &aus>i& 4 ?8 4 Kota 4 8999.99 4 A 4 C>aitali 4 ?@ 4 -um1ai 4 B@99.99 4 @ 4 /ardi& 4 ?B 4 )>o!al 4 C@99.99 4 D 4 Komal 4 ?? 4 -P 4 @@99.99 4 <====<==========<=====<===========<==========< T!E PA"#A$E SPE"I%I"ATION CREATE OR REP(ACE PACKAGE c_!ac&a'e AS == Adds a customer PROCEDURE addCustomer(c_id customers.id%t !eE c_name customers.name%t !eE c_a'e customers.a'e %t !eE c_addr customers.address %t !eE c_sal customers.salar %t !e"# == Remo6es a customer PROCEDURE delCustomer(c_id customers.id%T*PE"# ==(ists all customers PROCEDURE listCustomer# E$D c_!ac&a'e# % *hen the above code is e(ecuted at SQL prompt, it creates the above package and displays !ollowing result Pac&a'e created. "REATIN$ T!E PA"#A$E BO&' CREATE OR REP(ACE PACKAGE )OD* c_!ac&a'e AS PROCEDURE addCustomer(c_id customers.id %t !eE c_name customers.name %t !eE c_a'e customers.a'e %t !eE c_addr customers.address%t !eE c_sal customers.salar

4 4 4 4 4

%t !e" +S )EG +$ +$SERT +$TO customers (idEnameEa'eEaddressEsalar " FA(UES(c_idE c_nameE c_a'eE c_addrE c_sal"# E$D addCustomer# PROCEDURE delCustomer(c_id customers.id %t !e" +S )EG+$ DE(ETE ,RO- customers ./ERE id 0 c_id# E$D delCustomer#

TUTORIALSPOINT Simply Easy Learning 94

Page

PROCEDURE listCustomer +S CURSOR c_customers is SE(ECT name ,RO- customers# T*PE c_list is TA)(E O, customers.name%t !e# name_list c_list 30 c_list("# counter inte'er 309# )EG+$ ,OR n +$ c_customers (OOP counter 30 counter <7# name_list.eGtend# name_list(counter" 30 n.name# d1ms_out!ut.!ut_line(2Customer(2 44counter44 2"244 name_list(coun ter""# E$D (OOP# E$D listCustomer# E$D c_!ac&a'e# % Above e(ample makes use o! nested table which we will discuss in the ne(t chapter. *hen the above code is e(ecuted at SQL prompt, it produces !ollowing result Pac&a'e 1od created.

USIN$ T!E PA"#A$E "he !ollowing program uses the methods declared and de!ined in the package c5package. DEC(ARE code customers.id %t !e30 H# )EG+$ c_!ac&a'e.addcustomer(BE 2RaInis>2E ?@E 2C>ennai2E 8@99"# c_!ac&a'e.addcustomer(HE 2Su1>am2E 8?E 2Del>i2E B@99"# c_!ac&a'e.listcustomer# E$D# c_!ac&a'e.delcustomer(code"# % c_!ac&a'e.listcustomer#

*hen the above code is e(ecuted at SQL prompt, it produces !ollowing result Customer(7"3 Rames> Customer(?"3 K>ilan Customer(8"3 &aus>i& Customer(A"3 C>aitali Customer(@"3 /ardi& Customer(D"3 Komal Customer(B"3 RaInis> Customer(H"3 Su1>am Customer(7"3 Rames> Customer(?"3 K>ilan Customer(8"3 &aus>i& Customer(A"3 C>aitali Customer(@"3 /ardi& Customer(D"3 Komal Customer(B"3 RaInis> P(%S:( !rocedure successfull com!leted

You might also like