PL/SQL Programming Concepts Guide
PL/SQL Programming Concepts Guide
6LOCKK 3 seelions
Decdanaions Ee cul
Commands Excapuon
lenal (ma ndalo ry) Hanodun
ndosed
opTioned
KaweAls w
DECLARE
haniakun
BEGIN and Karwe
subp END Ex CEPTIO
RAmy
mtany *uplin
re laLA
hat hundl
PLJsL Stalimtnls
AOAms
AOa m
[Link] otteasl-
os, ma
..
a VL
Cmmand inol'eal
sheldbe
nattung
nlh a e n cato
E y PLSL Stemint ends
Of A PLSQL BLOCk
ASIC STRUCTURE
Stucti
DECLARE
ouclanatuni Cectun
BEGIN
Kexecutabe mma nds)
vaicharl(2o) DEfmnT
DEFAULT
ExCEPTION
or oleulk kaypsan
ttytusi hardlirg > ichaur H a
( t o )
&4oody
END
nay rullo
DECLARE koneld
Hl
musaas
vtha2 (2.:
9 ssigmment ofsualm
BEGIN
un (mesage)
tiat olbyns -Buwhut. puk. va
END
USER DEPINED SUBTY PES
PL/SQL anohu daalypa
Subljpe2 subu
Called
bauy F)
r u b p a Bato
SyeTYPE CHARA CTER S CHAR
b a n
y r s
Salukatm ami
musaL
BEGIN
SalulalLon := Radu'
L/s9
Aings: walm to hu weuld
kalubithos 1
ne Hello
lms- ukput. put.
autng) CPncabenalon
END onalar
SUBPROG RAM
SubpADam u a rigram hat oms o paiviculay
ask
Thut ubpora ms a Cmbinud6. om longi
u Celtd amodulay olun'.
a w .TKis
anolhiv
ubnaam Com be tnuohed
PL/sQL Subproams
Paoudu
unelLons
t h u ubproams a
heliwn a do-ne
au
nalu dosetty
wed Mauy
anly
Cnmyule r mluun alion
uau.
ASukpugam an be usali
hn
CREATE PUNCTION
& oklld
Stalu nuw
a
u lovad un DROP PA CkA 6£ Ltalemu!.
Edatobon eon
dili wnth DROP
PRoCEPURE
PROPFUNCTIO
AoLdul -nan
nlay
CREATING A PROCERURE
an txisty Jocas
Qllews modyying
non
fkocEDUREoudua.
CREATE [o REPLACEJ
namt CN oUTI 0UTJ p, 7 c
(pua mul J
ohonal
BEGqIN
K Aouduu body
nàm
ENQ bADeduu.
xanp
CREATE PROCEAURE m g
. AS
BEGIN [ Hllo wald")
dbms buhput. put-
END
_ocduut
Exeun
BEGIN 7
END,
lulng _oduus
.
BAO? ?ROCERURE 4g
6EhN
t DAOP PRDLEQURE ak
ENO
PROGRAMS
PARA METER
MOR ES IN PL/SL
OUT IN OUTT
IN
t d u s ualus to
olu t
Collns
t ( h e auisarameler alue
pdnl!
fnanuelirs
OUT MOR E
OF IN A two nalues ouolunu
EXAMPE mod lund
i n u m u m
ds IN
a m
taku 2 umbas
Unn OUT þanamlurs
un'un m
DECLARE
numben
bnumba ; N
IN
nmbes
numbe
numbea, y
(x N IS
AndMen nummbea)
PROCEQURE 2 bUT .
8EGIN THEN
If
ELSE
END IF
END
/
8FGIN
a23
b:45
ridrin a, b, t)
olbs. uhuk. Aut. Lns ( "Mui of (:3, 45);: '|l e).2
END
9uhpu
Huntmu (23, 4):13
ExAMPLE 2
DECLARE
a. umb
Ctaltt
PRoceERURE sguaneNum ( 0UT number) (s
6EGIN
FNO.
SBEGN
a 23
Squa Numla,
[Link]. uis ('Sgpau c23): '|la)
SyNTAY
CREATE OR REPLACE] FoweTtON euneturn- name
aamutir namu [IM|oUT | w our7 typ l : D
RETURN tndakalyjp
irslAS
6EG H
netunm- boely
END Ceunetim - nami
EXAMPLE dug 4callng a hmduni
kuins ta tetal numbt npya
puncti
e
poy
ceatoafnelen
CREATE PUNCTIQN ttal Bmp /
RETURNnumbu IS
tota numbea()i=0., .
BEGIN
C'totalbrpl)
enyluryu "ll ct
þut- lini ["Total
no.
dbns-put.
ENR
otal o%*gluys:
CURSORS
memey Rnorwn S
PAO
a polni o h i ontut area
eunsor
he Conlent area through
PLIS L Cmtols
t
CLoyY.
onnd .
nd
proes tme.
a a
Staimen, bnt .
of CURSORSS
TYPES 7
IMPLIeuT CURSORS
EXPLICI1T CURSORS
IMPLICT CUR$ORS
Otate wheusaur a
Arutomaliially
ial by
sttumnt u x eu
Sq Conliel put tursors qnd
no
Cannet
maton i t
UPDA TE
hetur a DML s tatenmen wS¬RT, an ImpltUF cursoy
s issud
Qnd DELE TE) wh h stattmunt
as&dela hotd ak e dals
dal hha
t asl
tunsoY hotd
for INSERT e
hds to be tmsulis iduntfu
1denlfu
h e
he.
&vDr
C
DATE 4 DELETE
UP wpuld e offeetd.
heus Jat
Curso
SQL un sov
M o s mun mpliu
athubul a
T fellowtn
TRUE Aa n INSERT
INSERT
-
hulians
PoUN Sab munt
OY DELE TE
VPDATE
the dali u hesent
( lduuhio
nod SELECT tNTostatumen Alinn
aelinns
yeus
me
etuans FALSE
0eswn's,
7 FouNND
2) NOTFOUND
an INsERT , UPDATE
TRUE
l i n n s
temun
or DEuTE Salimsa
Arlened
SELECT INTO
nOY0WS
heluns FASE
hetuuns FALSE for Implt'ut
ISOPOf EN was hi carsoy
CLNYOYY
clnoes
uinsers brtause Oatle aSgoualed
exeun9
Aurematuealy ali
sQL Sattnmes
Stalinmens
EXAM PLE
s scluut * Rom m,
dalr
table and
Ths hoann
salas y tath Cuetonnil by 500
St ROWtoVNTatubu
amd Use
lleamn? h mimber .
*
DECLARE
total ou umber(2)
BEGIN
UPDATE emp ET salary Salasy +5oo
IA S4%notoundy THEN
selachd).
dbis- eupuk. put. Lsl'no edtoshers
GLSIF ELSGIF s47,fnd THEN
total-wws 9 o w o u n Lmploy
dbms - eihut. put. uns (totalons i Cu ).
seleetis)
END IF
END
EXPLICIT CURSDRS
u dspned
Cmtex
Cnhot ou
qaunng
heuld tlud
okcelaralon eton
th DL/sqL stock.
statt mens wsth
SELE CT
u eacali
ont how
mel han
hahuans
Cunsor u
SYNTA for aling cxplueak
S alece- skatimunt
CuRSOR CUAsornam
uvsdus
cuwnsdy
an tzkliut
Weatir mh
stips
Relaung te u r sor
for tnlialur
aloalng mmimey
mey
he wsr foY
dali
wsor for mtweveng
altocali
allocali
3 erhn cwior
t ulase
4) Closu
THE CwRS DR
DECLARING he Cta
e usor olfnis
asoelala sELECT
Dalaung and he
nami
r namu
ttmunk
addsuss
SEECT mptd, ename,
CuRSOR(CLmp) IS
FROM wP
THE wRSOR
OPENIN
DPEN C- tmp,
THE CURSOR
FETCH ING
c-add
C-ep
NTO Ctd C-na mu
FETCH
CLOSE C Uanabla
e adnd
mia ha
ExAMPE h a b t nami
J , n
bEtARE asbha babtu
tnh7
BEGIN
OPEN i p 2 :opaug cursor
LOoP:
PETCH C- emp i C-a, cnamu, c-os / a
c u r s o
DML epenatioms
Of cod nam cdumnam ha woud sm te
pdalc
BeGIN
xeata
-
Nahm ns
n a 39L
ExcEPTIOoN Baumin
kekd, shieh is
Staliunb
Exupon -handlung
-
ENP
EXAMPLE 2
Selsc k hom Lm
a Aow en hu'g Po
wat
AoAn
Cmployu kas ho
would f for
or UPDA TE
DEETE S p u a l i o n s rormud
NSERT
This u w di splay
dsplay
bt
tn
n
4mploy
mploy
the old valus
Salay ofounee w
hew valuits
upelatur aw
xing Mcord.
pdaz Lmp Se Sal a
sal +So whuu
Unpro 1234
odsalauy z oo
Alw Salany I7 oo
SO
Salay ou'un
To DELETE A TklGGER
TAt3anNam2
PRoP TRIGER
ndi'enlo ha y ahd
AFTER
isguAg Staumink
e x euhng
Axe'g i stalimens
SYNTAX
tumu n an Cxiutung abl
imay ky
2)No Mull
3 thutk
anl
eren key
modi!
sumnna mt
Ktabluna nu)
Alli tabH
Kdahalpe depalt (null)
table
7 pdat
a elumnnami! = Kip4),
fablname ) &el
hsher
condstiim >,
cotumn.namu2 ) <Exp7
INQEXES .
i s t loals6a).
Mana k
Mads
Daell
kpamaen
Slct oealubn
nd. om
tue
alable's
NeBd
sloaac
eas
a efu
pfoumn
olefred
V alennd
hakrh
natrh ustr
Modh to
to localr SE SELECT
uuhdt
Chliua ceLAs SELaly
l e s r o l y
sel&(sease
a
t
A pheuu hi
npho
usealal
t
a
pnoderue &uiuwdom the lte.
wh wttch
Conlinls
l o a l k
o n d u n d _ u t
cstumn
hnday an
b
c o l u m
o n
m n
a a
Jnoixn9
nwu m 2p Matuy
* unskpendt
Cuals
t ndr be
wls
mau'
ou ma
2D onla
kom tabl
CBlumn
xauts
CLal
a dohues
Callud
Loal
hao u t r eoun
dalabare
(hi h (abe
odals nsuls r a i l
the
Jouhu
whn auonmaliallyy
Lngn
tue
'Aa' u o l u
v a l u l
dala
lou u t a t dala
dala i n d i c a l i s
Kos 18
Mcond
unngpn
L n e r l s
whers tabe
.
eyatly
Ostanid
N&EX unpu02
Pon
nD
Salra
VIEWS
eLald
a tabu
lur
a l
all
dals nwens
to
alleumns
nwssary a last
_ b
b t om
may
anMS s
dala lale.
Lenal
nal
Lalug
Mian
a 7
Augn e m i n l ,
F l u n l y
uo anowt olala
olala
to
wu
ul heuy
y
l l
Asdtundan dals
dalabase.
ek
tne
la