dbms ii chapter 2
TRANSCRIPT
-
8/13/2019 DBMS II Chapter 2
1/40
CHAPTER - 2
PL / SQL
STRUCTURE
2.1 INTRODUCTION
2.1.1 Performance
2.1.2 Portability
2.1.3 !at P"#$%" can &o for Pro'rammer(
2.2 THE P"#$%" $)NTA*
2.2.1 P"#$%" +loc, $trctre
2.2.2 P"#$%" C!aracter $et
2.2.3 ariable/
2.2.0 "iteral/
2.2. Comment/
2.3 P"#$%" DATA T)PE$ AND DEC"ARATION$
2.3.1 Declarin' ariable/
2.3.2 A//i'nin' a ale to ariable/
2.3.3 Declarin' a Con/tant
2.3.0 Pic,in' a ariable4/ Parameter from Table Cell
2.3. An I&entifier in P"#$%" bloc,
2.0 P"#$%" OPERATOR$
2.0.1 Arit!metic Oerator/
2.0.2 Comari/on Oerator/2.0.3 "o'ical Oerator/
2.0.0 $trin' Oerator/
2. CONDITIONA" CONTRO" IN P"#$%"
-
8/13/2019 DBMS II Chapter 2
2/40
2.5 INTRACTIE CONTRO" IN P"#$%"
2.5.1 "oo $tatement
2.5.2 !ile "oo
2.5.3 6oto $tatement
2.5.0 7or "oo
2.8 ERROR HAND"IN6 IN P"#$%"
2.8.1 U/er Define& E9cetion/
2.8.2 Pre Determine& Internal Pl#$%" E9cetion/.
2.: P"#$%" 7UNCTION$ AND PROCEDURE$
2.:.1 7nction/
2.:.2 Proce&re/
2.; DATA+A$E TRI66ER$
2.1< CUR$OR$
2.1
-
8/13/2019 DBMS II Chapter 2
3/40
2.1 INTRODUCTION:
$%" i/ a natral lan'a'e of t!e D+A. It &oe/ not !ae any roce&ral caabilitie/ /c!
a/ looin' an& branc!in'. 7or t!i/> Oracle roi&e/ t!e P"#$%" lan'a'e.
P"#$%" i/ really an e9ten/ion of $%". P"#$%" a&&/ to t!e o?er of $%" an& roi&e/
t!e /er ?it! all t!e facilitie/ of a ro'rammin' enironment. P"#$%" i/ a roce&ral lan'a'e
t!at ORAC"E &eeloe& a/ an e9ten/ion to /tan&ar& $%" in or&er to roi&e a ?ay to e9ecte
roce&ral lo'ic on t!e &ataba/e.
P"#$%" i/ an ORAC"E4/ roce&ral lan'a'e e9ten/ion to $%". Unli,e $%"> P"#$%"
i/ a roce&ral> not &eclaratie. T!i/ mean/ t!at P"#$%" co&e /ecifie/ e9actly !o? t!in'/ 'et
&one. P"#$%" al/o enable/ yo to embe& $%" /tatement/ ?it!in it/ roce&ral co&e.
P"#$%" bri&'e/ t!e 'a bet?een &ataba/e tec!nolo'y an& roce&ral ro'rammin'
lan'a'e/. It can be t!o'!t of a/ a &eeloment tool t!at e9ten&/ t!e facilitie/ of ORAC"E4/
$%" &ataba/e lan'a'e. Pro'rammer/ can /e it to create ro'ram/ for ali&ation an&
manilation of table &ata. ia P"#$%" !e can in/ert> &elete> &ate an& retriee table &ata a/
?ell a/ /e roce&ral tec!ni@e/ /c! a/ ?ritin' loo/ or branc!in' to anot!er bloc, of co&e. It
allo?/ !im to /e all t!e $%" &ata manilation /tatement/ a/ ?ell a/ tran/action roce//in' an&
cr/or control oeration/. P"#$%" bloc,/ can contain any nmber/ of $%" /tatement/. It
allo?/ yo to lo'ically 'ro a nmber of $%" /entence/ an& ar/e t!em to D+A a/ a /in'le
bloc,.
2.1.1 PERFORMANCE:
it!ot P"#$%" t!e D+A !a/ to roce// $%" /tatement/ one at a time. T!i/ re/lt/ in
call/ bein' ma&e to t!e D+A eac! time an $%" /tatement i/ e9ecte&. It /lo? &o?n/ table &ata
roce//in' con/i&erably. it! a P"#$%"> an entire bloc, of /tatement/ can be /ent to t!e
RD+=$ en'ine at any one time. It re&ce/ t!e traffic on t!e net?or,.
-
8/13/2019 DBMS II Chapter 2
4/40
P"#$%" can al/o be /e& in $%" form/. It/ roce&ral caabilitie/ can be /e& for
?ritin' comle9 tri''er/ t!at ?ill ali&ate &ata before it i/ lace& in t!e table.
2.1.2 PORTABILITY:
Alication/ ?ritten in P"#$%" are ortable to any comter !ar&?are an& oeratin'
/y/tem. !ere ORAC"E i/ oerational> !ence P"#$%" co&e bloc,/ ?ritten for t!e DO$ er/ion
of ORAC"E ?ill rn on it/ Uni9 er/ion> ?it!ot any mo&ification/ ma&e to it.
2.1.3 WHAT PL/SQL CAN DO FOR PROGRAMMERS ?
P"#$%" offer/ roce&ral met!o&/ of co&e ?ritin' for acce//in' t!e &ataba/e an&
manilatin' t!e &ata ?it! fle9ibility an& ea/e. T!i/ i/ beca/e P"#$%" /ort/ t!e follo?in'.
$%" &ata manilation /tatement/> by /in' ?!ic! table &ata can be
manilate&.
$%" tran/action roce//in' /tatement/> ?!ic! are treate& a/ a /in'le bloc,
by t!e D+A> /o t!at all c!an'e/ ma&e to t!e &ata in t!e table> are &one or n&one
all at once.
$%" fnction/ can be calle& from ?it!in P"#$%". T!e/e fnction/ can be
nmeric> c!aracter> &ate or &ata coner/ion fnction/. T!e fnction/ aailable in t!e$%" can be /e& in P"#$%" bloc,/.
SQL predicatesB are /e& in t!e ?!ere con&ition/ of $%" /entence/> for
&ata manilation. T!e re&icate to a ?!ere con&ition can con/i/t of a /imle
comari/on or of mltile comari/on/ /in' t!e lo'ical oeration/ AND> OR an&
NOT. Ot!er comari/on oerator/ li,e +ETEEN> I$ NU""> "IE> E*I$T$> etc.
can al/o be /e& to form re&icate/
P"#$%" allo?/ yo to &eclare ariable/ an& t!en /e t!em in bloc,/ of co&e. It i/ @ite
o//ible to /e ariable/ to /tore t!e re/lt/ of a @ery for later roce//in' or calclate ale/ an&
in/ert t!em into an ORAC"E table later. P"#$%" ariable can be /e& any?!ere> eit!er in $%"
/tatement/ or in P"#$%" bloc,/.
-
8/13/2019 DBMS II Chapter 2
5/40
-
8/13/2019 DBMS II Chapter 2
6/40
or&/ /e& in a P"#$%" bloc,/ are calle& lexical units. e can freely in/ert blan,
/ace/ bet?een le9ical nit/ in a P"#$%" bloc,/. T!e /ace/ !ae no effect on t!e P"#$%"
bloc,.
T!e or&inary /ymbol/ /e& in P"#$%" bloc,/ are
% & ' ( ) / * + , < = :
Comon& /ymbol/ /e& in P"#$%" bloc, are
*+ -, (, 6, *, +, : , )) >> ** ++
2.2.3. ARIABLES
ariable/ may be /e& to /tore t!e re/lt of a @ery or calclation/. ariable/ m/t be
&eclare& before bein' /e&. ariable/ in P"#$%" bloc, are name& ariable/. A ariable name
m/t be'in ?it! a c!aracter an& can be follo?e& by a ma9imm of 2; ot!er c!aracter/ Fariable
len't! i/ 3< c!aracter/G.
Re/ere& ?or&/ cannot be /e& a/ ariable name/ nle// enclo/e& ?it!in t!e &oble
@ote/. ariable/ m/t be /earate& from eac! ot!er by at lea/t one /ace or by a nctation
mar,.
T!e ca/e Fer#lo?erG i/ in/i'nificant ?!en &eclarin' ariable name/. $ace cannot be
/e& in a ariable name.
2.2.@. LITERALS
A literal i/ a nmeric ale or a c!aracter /trin' /e& to rere/ent it/elf. $o> literal/ can be
cla//ifie& into t?o tye/.
Nmeric literal/
Non- nmeric literal/ F/trin' literal/G
N !:
T!e/e can be eit!er inte'er/ or floatin' oint nmber/. If a float i/ bein' rere/ente&>
t!en t!e inte'er art m/t be /earate& from t!e float art by a erio& F .G.
-
8/13/2019 DBMS II Chapter 2
7/40
Inte'er/ 2 03 038 -8 etc
7loat/ 5.30 2E- an& NU"".
2.2.. COMMENTS
A comment line be'in/ ?it! a &oble !y!en F((G. In t!i/ ca/e t!e entire
line ?ill be treate& a/ a comment.
E9B -- T!i/ /ection erform/ /alary &ation.
T!e comment line be'in/ ?it! a /la/! follo?e& by an a/teri/, F#G till t!e
occrrence of an a/teri/, follo?e& by a /la/! F)/G. In t!i/ ca/e comment
line/ can be e9ten&e& to more t!an one line/.
E9-1B /) t!i/ i/ only for /er ro/e
?!ic! calclate/ t!e total /alary temorarily
an& /tore/ t!e ale in temJ/al )/
E9-2B /) T!i/ ta,e/ ro?/ from /) table E=P"O)EE )/
an& t on anot!er table )/
In t!e aboe comment> t!ere i/ a comment ?it!in an anot!er comment line>
-
8/13/2019 DBMS II Chapter 2
8/40
t!i/ i/ not allowedin P"#$%".
2.3. PL/SQL DATA TYPES AND DECLARATIONS:
P"#$%" /ort/ t!e /tan&ar& ORAC"E $%" &ata tye/. T!e &efalt &ata tye/ t!at
can be &eclare& in P"#$%" are
NUMBERB 7or /torin' nmeric &ata
SyntaxB ariable name NU=+ER Freci/ion> K/caleLG
reci/ion &etermine/ t!e nmber of /i'nificant &i'it/ t!at NU=+ER
can contain. $cale &etermine/ t!e nmber of &i'it/ to t!e ri'!t of t!e
&ecimal oint.
E9B NU=+ER F0>2G /tore/ 0230.5 no frt!er
a//i'nment to t!e con/tant/ i/ o//ible.
EB fJercent CON$TANT NU=+ER F3>2G BQ :.33
2.3.3 PICKING UP A ARIABLES PARAMETERS FROM A TABLE CELL
T!e ba/ic bil&in' bloc, of a table i/ a cell Fi.e. table4/ colmnG. !ile creatin' a
table /er attac!e/ certain attribte/ li,e &ata tye an& con/traint/. T!e/e attribte/ can be a//e&
on to t!e ariable/ bein' create& in P"#$%". T!i/ /imlifie/ t!e &eclaration of ariable/ an&
con/tant/.
7or t!i/ ro/e> t!e T)PE attribte i/ /e& in t!e &eclaration of a ariable ?!en
t!e ariable4/ attribte/ m/t be ic,e& from a table fiel& Fi.e. colmnG.
EB crrentJ/al emloyee./al T)PE
In t!e aboe e9amle> crrentJ/al i/ t!e ariable of P"#$%" bloc,. It 'et/ t!e &ata tye
an& con/traint/ of t!e colmn Ffiel&G sal belon' to t!e tableEmployee. Declarin' a ariable
?it! t!e T)PE attribte !a/ t?o a&anta'e/
)o &o not nee& to ,no? t!e &ata tye of t!e table colmn
If yo c!an'e t!e arameter/ of t!e table colmn> t!e ariable4/ arameter/ ?ill
c!an'e a/ ?ell.
-
8/13/2019 DBMS II Chapter 2
11/40
-
8/13/2019 DBMS II Chapter 2
12/40
Comari/on oerator/
"o'ical oerator/
$trin' oerator/
P"#$%" oerator/ are eit!er nary Fi.e. t!ey act on one ale#ariableG or binary Ft!ey act on
t?o ale/#ariable/G
[email protected] ARITHMETIC OPERATORS:
Arit!metic oerator/ are /e& for mat!ematical comtation/. T!ey are
[email protected] COMPARISON OPERATORS:
Comari/on oerator/ retrn a +OO"EAN re/lt> eit!er TRUE or 7A"$E. T!ey are
' A&&ition
( $btraction or Ne'ation F E9B (G
=ltilication
/ Dii/ion
)) E9onentiation oerator Fe9amle 1
-
8/13/2019 DBMS II Chapter 2
13/40
In a&&ition to t!i/ P"#$%" al/o roi&e/ /ome ot!er comari/on oerator/ li,e "IE> IN>
+ETEEN> I$ NU"" etc.
LIKEB Pattern-matc!in' oerator.
It i/ /e& to comare a c!aracter /trin' a'ain/t a attern. T?o ?il& car& c!aracter/
are &efine& for /e ?it! "IE> t!e Fercenta'e /i'nG an& F8 G n&er/core. T!e /i'n
matc!e/ any nmber of c!aracter/ in a /trin' an& F J G matc!e/ e9actly one.
E(1B ne?matc!e/ ?it! ne?yor,> ne?er/ey etc Fi.e. any /trin' be'innin' ?it!
t!e ?or& ne?G.
E(2B J J J&ay4 matc!e/ ?it! $n&ay> =on&ay an& 7ri&ay an& It ?ill not matc! ?it! ot!er &ay/ li,e Te/&ay4> e&ne/&ay4> T!r/&ay4 an&
$atr&ay4.
IN: C!ec,/ to /ee if a ale lie/ ?it!in a /ecifie& li/t of ale/. T!e /ynta9 i/
SyntaxB T!eJale KNOTL IN Fale1> ale2> ale3G
EB 3 IN F0> :> 8> > 3> an& 2G Retrn/ TRUE.
$n NOT IN F /at4> mon4> te4> ?e&4> /n4G Retrn/ TRUE.
BETWEENB C!ec,/ to /ee if a ale lie/ ?it! in a /ecifie& ran'e of ale.
SyntaxB t!eJale KNOTL +ETEEN lo?Jen& AND !i'!Jen&.
EB +ETEEN AND 1
-
8/13/2019 DBMS II Chapter 2
14/40
P"#$%" imlement/ 3 lo'ical oeration/ AND> OR an& NOT. T!e NOT oerator i/
nary oerator an& i/ tyically /e& to ne'ate t!e re/lt of a comari/on e9re//ion> ?!ere a/ t!e
AND an& OR oerator/ are tyically /e& to lin, to'et!er mltile comari/on/.
A AND + i/ tre only if A retrn/ TRUE an& + retrn/ TRUE el/e it i/ 7A"$E.
A OR + i/ TRUE if eit!er of A or + i/ TRUE. An& it i/ 7A"$E if bot! A an& +
are 7A$"E.
NOT A Retrn/ TRUE if A i/ 7A"$E
Retrn/ 7A"$E if A i/ TRUE.
EB F Q G AND F02 /at4G OR F2 Q 2G Retrn/ TRUE.
2.@.@ STRING OPERATORS:
P"#$%" !a/ t?o oerator/ /ecially &e/i'ne& to oerate only on c!aracter /trin' tye
&ata. T!e/e areLIKEan& F >V G Concatenation oerator.LIKE i/ a comari/on oerator an& i/
/e& to comare /trin'/ an& it i/ &i/c//e& in t!e reio/ /e//ion. Concatenation oerator !a/
follo?in' /ynta9.
SynatxB $trin'J1 VV /trin'J2$trin'J1 an& /trin'J2 bot! are /trin'/ an& can be a /trin' con/tant/> /trin' ariable/ or
/trin' e9re//ion/. T!e concatenation oerator retrn/ a re/ltant /trin' con/i/tin' of all t!e
c!aracter/ in /trin'J1 follo?e& by all t!e c!aracter/ in /trin'J2.
E9 B C!an&ra4 VV 4/!e,!ar4 Retrn/ C!an&ra/!e,!ar4
AQ4En'ineerin'4 +Q4Colle'e4 CQARCHAR2 F i.e. aaa4 not e@al to
AAA4.
NOTE-! ORAC"E !a/ /ome bilt in fnction/ t!at are &e/i'ne& to conert from one
&ata tye to anot!er &ata tye.
-
8/13/2019 DBMS II Chapter 2
15/40
TJ8!B Conert/ a c!aracter /trin' into &ate
TJ8B Conert/ a c!aracter /trin' to a nmber.
TJ8!B Conert/ eit!er a nmber or &ate to c!aracter /trin'.
EB ToJ&ate F1#1#;24> mm#&yy#4G Retrn/ mm-&&-yyyy4G Retrn/ Wanary 2 X;6;;;D;;4G Retrn/ X1> 232.;;
ToJc!ar F123.;;> ;;;D;;4G Retrn/ 123.;;.
2. CONDITIONAL CONTROL IN PL/SQL :
In P"#$%"> t!e if /tatement allo?/ yo to control t!e e9ection of a bloc, of
co&e. In P"#$%" ?e can /e t!e follo?in' if form/
I7 con&ition THEN /tatement/ END I7
I7 con&ition THEN /tatement/
E"$E /tatement/
END I7
I7 con&ition THEN /tatement/
E"$E I7 con&ition THEN
$tatement/
E"$E /tatement/
END I7
END I7
2. ITERATIE CONTROL IN PL/SQL :
P"#$%" roi&e/ iteratie control an& e9ection of P"#$%" /tatement/ in t!e
bloc,. T!i/ i/ t!e ability to reeat or /,i /ection/ of a co&e bloc,. 7ollo?in' are
t!e for tye/ of iteratie /tatement/ roi&e& by t!e P"#$%"
-
8/13/2019 DBMS II Chapter 2
16/40
T!e "oo /tatement
T!e HI"E "oo /tatement
T!e 6OTO /tatement
7OR "oo
2..1 LOOP STATEMENT:
A loo reeat/ a /e@ence of /tatement/. T!e format i/ a/ follo?/.
"OOP
$tatement/
END "OOP
T!e one or more P"#$%" /tatement/ can be ?ritten bet?een t!e ,ey ?or&/ "OOP
an& END "OOP. Once a "OOP be'in/ to rn> it ?ill 'o on foreer. Hence loo/ are
al?ay/ accomanie& by a con&itional /tatement/ t!at ,ee/ control on t!e nmber of
time/ it i/ e9ecte&. e can al/o bil& /er &efine& e9i/t/ from a loo> ?!ere re@ire&.
EB "OOP
Cntr B Q cntr 1
I7 cntr 1
-
8/13/2019 DBMS II Chapter 2
17/40
SyntaxB HI"E Con&ition i/ TRUE
"OOP
$tatement/
END "OOP
EB DEC"ARE
Cont NU=+ERF2G B Q
-
8/13/2019 DBMS II Chapter 2
18/40
T!e label i/ /rron&e& by &oble brac,et/ F** ++G an& label m/t not !ae a /emi
colon after t!e label name. T!e label name &oe/ not contain a /emi colon beca/e it i/ not a
P"#$%" /tatement. +t rat!er an i&entifier of a bloc, of P"#$%" co&e. )o m/t !ae at lea/t
one /tatement after t!e label ot!er?i/e an error ?ill re/lt. T!e 6OTO &e/tination m/t be in t!e
/ame bloc,> at t!e /ame leel a/ or !i'!er t!an t!e 6OTO /tatement it/elf.
EB I7 re/lt Q YfailY THEN
6OTO faile&J/t&
END I7
faile&J/t&
=e//a'e FY/t&ent i/ faile&YG
T!e entry oint of t!e &e/tination bloc, i/ &efine& ?it!in a/ /!o?naboe> i.e. label/ are ?ritten ?it!in t!e /ymbol ** ++. Notice t!at faile&J/t& i/ a
label an& it i/ not en&e& ?it! /emicolon F G.
2..3 FOR LOOP:
7OR loo ?ill allo? yo to e9ecte a bloc, of co&e reeate&ly ntil /ome
con&ition occr/. T!e /ynta9 of 7OR loo i/ a/ follo?/.
SyntaxB 7OR looJin&e9 IN K REER$EL lo?Jale .. Hi'!Jale "OOP
$tatement/ to e9ecte
END "OOP
T!e looJin&e9 i/ &efine& by oracle a/ a local ariable of tye inte'er. REER$E
allo?/ yo to e9ecte t!e loo in reer/e or&er. T!e lo?Jale ..Hi'!Jale i/ t!e ran'e
to e9ecte t!e loo. T!e/e can be con/tant/ or ariable/. T!e line m/t be terminate& ?it!
loo ?it! no /emicolon at t!e en& of t!i/ line. )o can li/t t!e /tatement/ to be e9ecte&
ntil t!e loo i/ e9ecte& i/ ealate& to fal/e.
EB 7OR Jcont IN 1 .. "OOP
=e//a'e FYfor loo e9ecte/YG
END "OOP
In t!e aboe e9amle t!e me//a'e Yfor loo e9ecte/Y i/ &i/laye& fie time/.
-
8/13/2019 DBMS II Chapter 2
19/40
e can terminate t!e 7OR loo ermanently /in' E*IT /tatement ba/e& on
/ome +OO"EAN con&ition. Ne/tin' of 7OR loo can al/o be allo?e& in P"#$%". T!e
oter loo e9ecte& once> t!en t!e inner loo i/ e9ecte& a/ many time/ a/ t!e ran'e
in&icate/> an& t!en t!e control i/ retrne& to t!e oter loo ntil it/ ran'e e9ire/.
EB 7OR otJcont IN 1..2 "OOP
7OR inJcont IN 1..2 "OOP
=e//a'e FYne/te& for looYG
END "OOP
END "OOP
In t!e aboe e9amle t!e me//a'e Yne/te& for looY i/ &i/laye& for time/.
"et / &i/c// /ome e9amle/ from t!e n&er/tan&in' !o? to ?rite a P"#$%"
bloc, /trctre. Here ?e a//me t!at a table calle& ZE=PZ i/ create& an& t!e &ata/ are
alrea&y in/erte& into it.
T! ! : EMP
Create table E=P
F emJno NU=+ER F3G>
name ARCHAR2 F1G>
/alary NU=+ER F5>2G>
&et ARCHAR2 F1G>
&i ARCHAR2 F2G G
EXAMPLE(1B
DEC"ARE
nm NU=+ER F3G
/al em./alary T)PE
emJname em.name T)PE
cont NU=+ER F2G B Q 1
/tartin'Jem CON$TANT NU=+ERF3G B Q 130
+E6IN
-
8/13/2019 DBMS II Chapter 2
20/40
$E"ECT name> /alary INTO emJname> /al
7RO= E=P
HERE emJno Q /tartin'Jem
HI"E /al 0 name> /alary INTO
Nm> emJname> /al 7RO= E=P
HERE emJno 21
-
8/13/2019 DBMS II Chapter 2
21/40
T!i/ e9amle a//me/ t!e e9i/tence of table accont/ create& by /in' t!e
follo?in' $%" /tatement/.
Create table Accont/
FaccntJi& NU=+ERF3G>
name ARCHAR2F2G>
bal NU=+ERF5>2G G
PL/SQL J:
DEC"ARE
acctJbalance NU=+ERF5>2G
acct CON$TANT NU=+ERF3G B Q 312&ebitJamt CON$TANT NU=+ERF>2G B Q an& /tore/ t!at in a ariable acct%balance.
-
8/13/2019 DBMS II Chapter 2
22/40
If /tatement c!ec,/ acct%balancefor /fficient amont before &ebitin'. It &ate/
t!e table (ccountsif it !a/ /fficient amont in t!e balance> el/e it &i/lay/ a me//a'e
intimatin' in/fficient fn& in t!e accont of /ecifie& accnt%id.
EXAMPLE(3:
T!i/ e9amle a//me/ t?o table/> ?!ic! are create& by follo?in' /tatement/.
Create table Inentory
F ro&Jno NU=+ER F5G>
ro&ct ARCHAR2 F1G>
@antity NU=+ER FG G
Create table Prc!a/eJrecor&
F me/' ARCHAR2 F
&Jate DATE G
PL/SQL JB
DEC"ARE
nmJinJ/tac, NU=+ERFG
+E6IN
$E"ECT @antity INTO nmJinJ/tac,7RO= Inentory HERE ro&ct Q Y'a/,etY
I7 nmJinJ/tac, < THEN
UPDATE Inentory $ET @antity B Q @antity - 1
HERE ro&ct Q Y'a/,etY
IN$ERT INTO Prc!a/eJrecor&
A"UE$ FY One 'a/,et rc!a/e&Y> /y/&ateG
E"$E
IN$ERT INTO Prc!a/eJrecor&
A"UE$ FYno 'a/,et aailabelY>/y/&ateG
=e//a'e F Yt!ere are no more 'a/,et in /tac,Y G
END I7
Commit
-
8/13/2019 DBMS II Chapter 2
23/40
END
T!e aboe bloc, of P"#$%" co&e &oe/ t!e follo?in'
It &etermine/ !o? many 'a/,et/ are left in /tac,.
If t!e nmber left in /taff i/ 'reater t!an Mero> it &ate/ t!e inentory to
reflect t!e /ale of a 'a/,et.
It /tore/ t!e fact t!at a 'a/,et ?a/ rc!a/e& on a certain &ate.
If t!e /toc, aailable i/ Mero> it /tore/ t!e fact t!at t!ere are no more
'a/,et/ for /ale on t!e &ate on ?!ic! /c! a /itation occrre&.
2. ERROR HANDLING IN PL/SQL :
P"#$%" !a/ t!e caability of &ealin' ?it! t!e error/ t!at ari/e ?!ile e9ectin' a
P"#$%" bloc, of co&e. It !a/ a nmber of con&ition/ t!at are re ro'ramme& in to it t!at
are reco'niMe& a/ error con&ition/. T!e/e are calle& internally defined exceptions. )o can
al/o ro'ram P"#$%" to reco'niMe /er-&efine& e9cetion/.
T!ere are t?o &ifferent tye/ Error con&ition/ F E9cetion/G.
/er &efine& error con&ition/ # e9cetion/.
Pre&etermine& internal P"#$%" e9cetion/.
2..1 USER DEFINED EXCEPTIONS:
U/er can ?rite a /et of co&e> ?!ic! i/ to be e9ecte& ?!ile error occr/ ?!en
e9ectin' a P"#$%" bloc, of co&e. T!e/e /et of co&e are calle& user-defined exceptions>an& t!e/e are lace& in t!e la/t /ection of P"#$%" bloc, calle& E*CEPTION$.
T!e met!o& /e& to reco'ni/e /er-&efine& e9cetion/ i/ a/ follo?/
Declare a /er &efine& e9cetion in t!e &eclaration /ection of P"#$%" bloc,.
-
8/13/2019 DBMS II Chapter 2
24/40
In t!e main ro'ram bloc, for t!e con&ition/ t!at nee&/ /ecial attention>
e9ecte a RAI$E /tatement.
Reference t!e &eclare& e9cetion ?it! an error !an&lin' rotine in
E*CEPTION /ection of P"#$%" bloc,.
RAI$E /tatement act/ li,e CA"" /tatement of !i'! leel lan'a'e/. It !a/ 'eneral format
RAI$E name of e9cetion
!en RAI$E /tatement i/ e9ecte&> it /to/ t!e normal roce//in' of P"#$%" bloc,
of co&e an& control a//e/ to an error !an&ler bloc, of t!e co&e at t!e en& of P"#$%"
ro'ram bloc, F E*CEPTION /ection G.
An e9cetion &eclaration &eclare/ a name for /er &efine& error con&ition/ t!at t!e
P"#$%" co&e bloc, reco'niMe/. It can only aear in t!e DEC"ARE /ection of t!e P"#$%" co&e
?!ic! recee&e/ t!e ,ey ?or& +E6IN.
EXAMPLEB
DEC"ARE
---------------
MeroJcommi//ion E9cetion
---------------+E6IN
-----------------
I7 commi//ion Q < THEN
RAI$E MeroJcommi//ion
------------------------
E*CEPTION
HEN MeroJcommi//ion THEN
Proce// t!e error
END
-
8/13/2019 DBMS II Chapter 2
25/40
E9cetion !an&ler Ferror !an&ler bloc, G i/ ?ritten bet?een t!e ,ey ?or&/ E*CEPTION
an& END. T!e e9cetion !an&lin' art of a P"#$%" co&e i/ otional. T!i/ bloc, of co&e
/ecifie/ ?!at action !a/ to be ta,en ?!en t!e name& e9cetion con&ition occr/.
T!e namin' conention for e9cetion name are e9actly t!e /ame a/ t!o/e for ariable/ or
con/tant/. All t!e rle/ for acce//in' an e9cetion from P"#$%" bloc,/ are /ame a/ t!o/e for
ariable/ an& con/tant/. Ho?eer> it /!ol& be note& t!at e9cetion/ cannot be a//e& a/
ar'ment/ to fnction/ or roce&re/ li,e ariable/ or con/tant/.
2..2 PREDETERMINED INTERNAL PL/SQL EXCEPTIONS:
T!e ORAC"E /erer &efine/ /eeral error/ ?it! /tan&ar& name/. Alt!o'! eery
ORAC"E error !a/ a nmber> t!e error m/t be referre& by name. P"#$%" !a/ re&efine& /ome
common ORAC"E error/ an& e9cetion/. $ome of t!em are 'ien belo?B
NOJDATAJ7OUND Rai/e& ?!en a /elect /tatement retrn/ Mero ro?/.
TOOJ=AN)JRO$ Rai/e& ?!en a /elect /tatement retrn/ more t!an one ro?/.
A"UEJERROR Rai/e& ?!en t!ere i/ eit!er a &ata tye mi/matc! or if
t!e /iMe i/ /maller t!an re@ire& /iMe.
INA"IDJNU=+ER Rai/e& ?!en coner/ion of a nmber to a c!aracter /trin'
faile&.
[EROJDIIDE Rai/e& ?!en attemte& to &ii&e by Mero.
PRO6RA=JERROR Rai/e& if P"#$%" enconter/ an internal roblem.
$TORA6EJERROR Rai/e& if P"#$%" rn/ ot of memory or if memory i/
corrte&.
DUPJA"JONJINDE* Rai/e& ?!en attemte& to in/ert or &ate a &licate into
a colmn t!at !a/ ni@e in&e9. INA"IDJCUR$OR Rai/e& ?!en ille'al cr/or oeration ?a/ attemte&.
CUR$ORJA"READ)JOPEN Rai/e& ?!en attemte& to oen a cr/or t!at ?a/
reio/ly oene&.
NOTJ"O66EDJON Rai/e& ?!en a &ataba/e call ?a/ ma&e ?it!ot bein'
-
8/13/2019 DBMS II Chapter 2
26/40
lo''e& into ORAC"E.
"O6INJDENIED Rai/e& ?!en lo'in to ORAC"E faile& beca/e of inali&
/ername an& a//?or&.
OTHER$ T!i/ ?ill be rai/e& ?!en t!e all ot!er e9cetion/ faile&
to catc! t!e error/.
It i/ o//ible to /e HEN OTHER$ cla/e in t!e e9cetion !an&lin' art of t!e
P"#$%" bloc,. It ?ill ta,e care of all e9cetion/ t!at are not ta,en care of in t!e co&e.
T!e /ynta9 for e9cetion !an&lin' ortion of P"#$%" bloc, i/ a/ follo?/B
E*CEPTION
HEN e9cetionJ1 THEN $tatement/
HEN e9cetionJ2 THEN $tatement/
- - --- ---- -- ---
END
In t!i/ /ynta9> e9cetionJ1 an& e9cetionJ2 are t!e name/ of e9cetion/ Fmay be
re&efine& or /er-&efine& G. $tatement/ in t!e P"#$%l co&e t!at ?ill be e9ecte& if t!e e9cetion
name i/ /ati/fie&.
EXAMPLE(1:
T!i/ e9amle ?rite/ P"#$%" co&e for ali&atin' accnt%id of Accountstable /o t!at it
m/t not be left blan,> if it i/ blan, cr/or /!ol& not be allo?e& to moe to t!e ne9t fiel&.
DEC"ARE
noJale e9cetion
+E6IN
I7 B Accont/.accntJi& I$ NU"" THEN
RAI$E noJale
E"$E
ne9tJfiel&
-
8/13/2019 DBMS II Chapter 2
27/40
END I7
E*CEPTION
HEN noJale THEN
=e//a'e F Yaccont i& cannot be> blan, Plea/e enter ali& &ata SSS Y G
'oJfiel& F B /y/tem.cr/orJfiel& G
END
In t!e aboe e9amle accnt%idfiel& of(ccounts table i/ c!ec,e& for NU"" ale. If it i/
NU""> t!en RAI$E /tatement call/ e9cetion !an&ler no%)alue. T!i/ e9cetion name no%)alue
i/ &eclare& in DEC"ARE /ection an& &efine& in t!e E*CEPTION /ection of P"#$%" bloc, by
/in' HEN /tatement. no%)aluei/ a /er-&efine& e9cetion.
EXAMPLE(2:
DEC"ARE
balance Accont/.bal T)PE
acontJnm Accont/.accntJi& T)PE
+E6IN
$E"ECT accntJi& bal INTO accontJnm> balance
7RO= Accont/ HERE accntJi& t!e ORAC"E /erer ?ill rai/e
t!i/ e9cetion ?!en t!ere i/ no &ata in balan& accnt%idfiel&.
-
8/13/2019 DBMS II Chapter 2
28/40
2. PL/SQL FUNCTIONS AND PROCEDURES :
P"#$%" allo?/ yo to &efine fnction/ an& roce&re/. T!e/e are /imilare to fnction/
an& roce&re/ &efine& in any ot!er lan'a'e/> an& t!ey are &efine& a/ one P"#$%" bloc,.
2..1 FUNCTIONSB
T!e /ynta9 for &efinin' a fnction i/ a/ follo?/ B
7UNCTION name K Far'ment-li/tG L RETURN &ata-tye \I$> A$]
ariable-&eclaration/
+E6IN
Pro'ram-co&e
K E*CEPTION
error-!an&lin'-co&eL
END
In t!i/ /ynta9>
name T!e name yo ?ant to 'ie t!e fnction.
ar'ment-li/t "i/t of int anor ott arameter/ for t!e fnction/.
&ata-tye. T!e &ata tye of t!e fnctionY/ retrn ale.
ariable-&eclaration/ !ere yo &eclare any ariable/ t!at are local to t!e fnction.
ro'ram-co&e !ere yo ?rite P"#$%" /tatement/ t!at ma,e t!e fnction.
error-!an&lin'-co&e !ere yo ?rite any error !an&lin' rotine.
Notice t!at t!e fnction bloc, i/ /imilar to t!e P"#$%" bloc, t!at ?e &i/c//e& earlier.
T!e ,ey?or& DEC"ARE !a/ been relace& by 7UNCTION !ea&er> ?!ic! name/ t!e
fnction> &e/cribe/ t!e arameter an& in&icate/ t!e retrn tye.
7nction/ can be calle& by /in' nameF ar'ment li/t G
E!V:
7UNCTION c!ec,FbJe9 in +OO"EAN>
TreJnmber in NU=+ER>
-
8/13/2019 DBMS II Chapter 2
29/40
7al/eJnmber in NU=+ERG
RETURN NU=+ER I$
+E6IN
I7 bJe9 THEN RETURN treJnmber
E"$E
RETURN fal/eJnmber
END I7
END
T!e aboe fnction can be calle& a/ follo?/.
C!ec, F 2 1> 1 > A$]
ariable &eclaration
+E6IN
Pro'ram co&e
KE*CEPTION
Error !an&lin' co&e L
END
Here namei/ t!e name t!at yo ?ant to 'ie t!e roce&re an& all ot!er are /imilar to
fnction &eclaration. Proce&re &eclaration re/emble/ a fnction &eclaration e9cet t!at t!ere i/
no &ata tye an& ,ey ?or& PROCEDURE i/ /e& in/tea& of 7UNCTION.
EB PROCEDURE /?an FAIN OUT NU=+ER> BIN OUT NU=+ERG I$
TemJnm NU=+ER
-
8/13/2019 DBMS II Chapter 2
30/40
+E6IN
TemJnm B Q A
A B Q +
+ B Q temJnm
END
T!e aboe roce&re can be calle& a/ follo?/.
$?an F3>0G
$?an F-5>8G
2.$ DATABASE TRIGGERS :
P"#$%" can be /e& to ?rite &ata ba/e tri''er/. Tri''er/ are /e& to &efine co&e t!at i/
e9ectefire& ?!en certain action/ or eent occr. At t!e &ata ba/e leel> tri''er/ can be &efine&for eent/ /c! a/ in/ertin' a recor& into a table> &eletin' a recor&> an& &atin' a recor&.
T!e tri''er &efinition con/i/t/ of follo?in' ba/ic art/.
T!e eent/ t!at fire/ t!e tri''er
T!e &ataba/e table on ?!ic! eent m/t occr
An otional con&ition controllin' ?!en t!e tri''er i/ e9ecte&
A P"#$%" bloc, containin' t!e co&e to be e9ecte& ?!en t!e tri''er i/ fire&.
A tri''er i/ a &ata ba/e obect> li,e a table or an in&e9. !en yo &efine a tri''er> it
become/ a art of t!e &ataba/e an& it i/ al?ay/ i/ e9ecte& ?!en t!e eent for ?!ic! it i/ &efine&
occr/.
$ynta9 for creatin' a &ata ba/e tri''er i/ /!o?n belo?.
CREATE K or REP"ACE L TRI66ER tri''er-name
\ +E7ORE V A7TER ] erb-li/t ON table-name
K 7OR EACH RO K HEN con&ition L L
DEC"ARE
Declaration/
-
8/13/2019 DBMS II Chapter 2
31/40
-
8/13/2019 DBMS II Chapter 2
32/40
P"#$%" cr/or/ roi&e a ?ay for yor ro'ram to /elect mltile ro?/ of &ata from t!e
&ataba/e an& t!en to roce// eac! ro? in&ii&ally. Cr/or/ are P"#$%" con/trct/ t!at enable
yo to roce//> one ro? at a time> t!e re/lt/ of a mlti ro? @ery.
ORAC"E /e/ ?or, area/ to e9ecte $%" /tatement/> P"#$%" allo?/ /er to name
riate ?or, area/ an& acce// t!e /tore& information. T!e P"#$%" con/trct/ to i&entify eac!
an& eery ?or, area /e& by $%" i/ calle& a &ursor.
T!ere are 2 tye/ of cr/or/.
Imlicit cr/or/
E9licit cr/or/
Imlicit cr/or/ are &eclare& by ORAC"E for eac! UPDATE> DE"ETE an& IN$ERT
$%" comman&/. E9licit cr/or/ are &eclare& an& /e& by t!e /er to roce// mltile ro?>
retrne& by $E"ECT /tatement.
T!e /et of ro?/ retrne& by a @ery i/ calle& t!e (cti)e Set. It/ /iMe &een&/ on t!e
nmber of ro?/ t!at meet t!e /earc! criteria of t!e $%" @ery. T!e &ata t!at i/ /tore& in t!e
cr/or i/ calle& t!e(cti)e *ata Set.
ORAC"E cr/or i/ a mec!ani/m /e& to ea/ily roce// mltile ro?/ of &ata. Cr/or/
contain a ointer t!at ,ee/ trac, of crrent ro? bein' acce//e&> ?!ic! enable/ yor ro'ram to
roce// t!e ro?/ at a time.
EXAMPLEB
!en a /er e9ecte/ t!e follo?in' $E"ECT /tatement
$E"ECT emJno> emJname> ob> /alary
7RO= emloyee
HERE &et Q Y!y/ic/Y
T!e re/ltant &ata/et ?ill be &i/laye& a/ follo?/
1230 A. N. $!aran A//t. Profe//or 22>
-
8/13/2019 DBMS II Chapter 2
33/40
2.1#.1 EXPLICIT CURSOR MANAGEMENTB
T!e follo?in' are t!e /te/ to /in' e9licitly &efine& cr/or/ ?it!in P"#$%"
Declare t!e cr/or
Oen t!e cr/or
7etc! &ata from t!e cr/or
Clo/e t!e cr/or
D! J :
Declarin' a cr/or enable/ yo to &efine t!e cr/or an& a//i'n a name to it. It !a/ follo?in'
/ynta9.
CUR$OR cr/or-name
I$ $E"ECT /tatement
E: CUR$OR cJname I$
$E"ECT emJname 7RO= Em HERE &et Q Y!y/ic/Y
OV ! J:
Oenin' a cr/or e9ecte/ t!e @ery an& i&entifie/ t!e actie /et t!at contain/ all t!e
ro?/> ?!ic! meet t!e @ery /earc! criteria.
$ynta9 B
OPEN cr/orJname
EB
OPEN cJname
Oen /tatement retriee/ t!e recor&/ from t!e &ataba/e an& lace/ it in t!e cr/or Friate
$%" areaG.
F !! J J:
-
8/13/2019 DBMS II Chapter 2
34/40
T!e fetc! /tatement retriee/ t!e ro?/ from t!e actie /et one ro? at a time. T!e fetc! /tatement
i/ /e& /ally /e& in connction ?it! iteratie roce// F looin' /tatement/ G. In iteratie roce// t!e
cr/or a&ance/ to t!e ne9t ro? in t!e actie /et eac! time t!e fetc! comman& i/ e9ecte&. T!e fetc!
comman& i/ t!e only mean/ to nai'ate t!ro'! t!e actie /et.
$ynta9 B 7ETCH cr/or-name INTO recor&-li/t
Recor&-li/t i/ t!e li/t of ariable/ t!at ?ill receie t!e colmn/ Ffiel&/ G from t!e actie /et.
E9B "OOP
-----------
------------
7ETCH cJname INTO name
-----------
END "OOP
CJ ! J :
Clo/in' /tatement clo/e/#&eactiate/#&i/able/ t!e reio/ly oene& cr/or an& ma,e/ t!e actie
/et n&efine&. Once it i/ clo/e&> yo cannot erform any oeration/ on it. Once a cr/or i/ clo/e&> t!e
/er can reoen t!e cr/or by /in' Oen /tatement.
$ynta9 B C"O$E cr/orJname
EB C"O$E cJname
EXAMPLE(1:
T!e HRD mana'er !a/ &eci&e& to rai/e t!e /alary for all t!e emloyee/ in t!e !y/ic/
&eartment by
-
8/13/2019 DBMS II Chapter 2
35/40
&et arc!ar F1G
ob arc!ar F1G
/alary nmber F5>2G
T!: V8!
emJco&e arc!arF12G
SJJ:
DEC"ARE
CUR$OR cJem I$
$E"ECT emJco&e> /alary 7RO= emloyee
HERE &et Q Y!y/ic/Y
/trJemJco&e emloyee.emJco&e T)PE
nmJ/alary emloyee./alary T)PE
+E6IN
OPEN cJem
"OOP
7ETCH cJem INTO /trJemJco&e> nmJ/alary
UPDATE emloyee $ET /alary B Q nmJ/alary FnmJ/alary
-
8/13/2019 DBMS II Chapter 2
36/40
2.1#.2 EXPLICIT CURSOR ATTRIBUTES:
ORAC"E roi&e/ certain attribte/#cr/or ariable/ to control t!e e9ection of t!e
cr/or. !eneer any cr/or Fe9licit or imlicit G i/ oene& an& /e&> ORAC"E create/ a /et of
for /y/tem ariable/ ia ?!ic! ORAC"E ,ee trac, of t!e Zcrrent /tat/Z of t!e cr/or.
Pro'rammer/ can acce// t!e/e ariable/. T!ey are
NOT 7OUNDB Ealate/ to TRUE if t!e la/t fetc! i/ faile& i.e. no more ro?/ are left.
Syntax! cr/orJname NOT 7OUND
7OUNDB Ealate/ to TRUE> ?!en la/t fetc! /ccee&e&
Syntax! cr/orJname 7OUND
I$OPENB Ealate/ to TRUE> if t!e cr/or i/ oene&> ot!er?i/e ealate/ to
7A"$E. Syntax! cr/orJname I$OPEN
ROCOUNTB Retrn/ t!e nmber of ro?/ fetc!e&.
SyntaxB cr/orJname ROCOUNT.
EXAMPLE:
DEC"ARE
JemJname arc!ar2F32G
J/alaryJrate nmberF5>2G
JayrollJtotal nmberF;>2G
JayJtye c!ar
CUR$OR cJem I$
$E"ECT emJname> ayJrate> ayJtye 7RO= emloyee
HERE emJ&et Q Y!y/ic/Y
+E6IN
I7 cJname I$OPEN THEN
RAI$E
-
8/13/2019 DBMS II Chapter 2
37/40
notJoene&
E"$E
OPEN cJem
"OOP
7ETCH cJem INTO JemJname> J/alaryJrate> JayJtye
E*IT HEN cJem NOT7OUND
I7 JayJtye Q Y$Y THEN
JayrollJtotal B Q FJ/alaryJrate 1.2 G
E"$E
JayrollJtotal B Q FJ/alaryJrate 0
-
8/13/2019 DBMS II Chapter 2
38/40
1. $%" i/ a roce&ral lan'a'e.
2. P"#$%" i/ ortable in natre.
3. P"#$%" ?or& or ariable len't! i/ 2; c!aracter/.
0. P"#$%" ariable name/ are ca/e /en/itie.
. ^Oracle_ i/ a nonnmeric literal.
5. arc!ar2 i/ /e& to &eclare ariable len't! c!aracter &ata tye.
8. ROT)PE i/ /e& to ic, a ariable4/ arameter from a table cell.
:. TOJCHAR conert/ eit!er a nmber or &ate to c!aracter /trin'.
;. "abel/ are ?ritten ?it! in t!e /ymbol/ .
1
-
8/13/2019 DBMS II Chapter 2
39/40
C. ANSWER THE FOLLOWING: (
1. E9lain !o? P"#$%" oercome/ t!e &ra?bac, of $%".
2. E9lain !o? P"#$%" roi&e/ fle9ibility to ro'rammer/.
3. E9lain !o? comment line/ are ?ritten in P"#$%".
0. E9lain P"#$%" &efalt &ata tye/.
. =ention t!e a&anta'e/ of T)PE &eclaration.
5. E9lain t!e follo?in' P"#$%" oerator/
aG "IE bG IN cG +ETEEN.
8. E9lain any t?o P"#$%" /tatement/ t!at roi&e/ iteratie control
oer e9ection of co&e.
:. E9lain t!e &ifferent tye/ of e9cetion/.;. Differentiate bet?een P"#$%" fnction/ an& roce&re/.
1
-
8/13/2019 DBMS II Chapter 2
40/40
1