Introduction to PL/SQL - homes.ieu.edu.trhomes.ieu.edu.tr/~catay/Oracle/PLSQL.pdfTell Me/Show Me...
Transcript of Introduction to PL/SQL - homes.ieu.edu.trhomes.ieu.edu.tr/~catay/Oracle/PLSQL.pdfTell Me/Show Me...
-
Copyright
2011,
Ora
cle
. A
ll rights
reserv
ed.
Intr
od
ucti
on
to
PL
/SQ
L
-
2
Intr
od
ucti
on
to
PL
/SQ
L
Copyright
2011,
Ora
cle
. A
ll rights
reserv
ed.
Wh
at
Will I L
earn
?
Ob
jec
tiv
es
In t
his
lesson,
you w
ill learn
how
to:
D
escribe P
L/S
QL
D
iffe
rentiate
betw
een S
QL a
nd P
L/S
QL
E
xpla
in t
he n
eed f
or
PL/S
QL
-
3
Intr
od
ucti
on
to
PL
/SQ
L
Copyright
2011,
Ora
cle
. A
ll rights
reserv
ed.
Wh
y L
earn
It?
Pu
rpo
se
PL/S
QL is O
racle
Corp
ora
tions
sta
nd
ard
pro
ce
du
ral la
ng
ua
ge
fo
r re
latio
na
l d
ata
ba
se
s.
To
de
scrib
e P
L/S
QL
yo
u le
arn
its
ch
ara
cte
ristics a
nd
ge
t b
ett
er
pre
pa
red
to
de
scrib
e t
he
diffe
ren
ce
s b
etw
ee
n P
L/S
QL
an
d S
QL
.
Un
de
rsta
nd
ing
th
e lim
ita
tio
ns o
f S
QL
will
he
lp y
ou
to
un
de
rsta
nd
w
hy the P
L/S
QL language is n
eeded.
Yo
u u
nd
ers
tan
d h
ow
im
po
rta
nt
it is n
ot
to lo
ose
a k
ey t
o y
ou
r h
ou
se
wh
en
yo
u a
re t
ryin
g t
o g
et
into
th
e h
ou
se
with
ou
t th
e k
ey.
-
4
Intr
od
ucti
on
to
PL
/SQ
L
Copyright
2011,
Ora
cle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
PL
/SQ
L D
es
cri
pti
on
P
roce
du
ral L
an
gu
ag
e e
xte
nsio
n t
o S
QL
It a
llow
s b
asic
pro
gra
m logic
and c
ontr
ol flow
to b
e c
om
bin
ed
with S
QL s
tate
ments
O
racle
pro
prie
tary
pro
gra
mm
ing
la
ng
ua
ge
It c
an b
e u
sed o
nly
with a
n O
racle
data
base o
r to
ol
P
roce
du
ral la
ng
ua
ge
It p
roduces a
result w
hen a
series o
f in
str
uctio
ns a
re f
ollo
we
d
3
GL
(th
ird
-ge
ne
ratio
n p
rog
ram
min
g la
ng
ua
ge
)
It is a
hig
h-level p
rog
ram
min
g la
ng
ua
ge
-
5
Intr
od
ucti
on
to
PL
/SQ
L
Copyright
2011,
Ora
cle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Str
uc
ture
d Q
ue
ry L
an
gu
ag
e (
SQ
L)
De
sc
rip
tio
n
Is
th
e p
rim
ary
la
ng
ua
ge
use
d t
o a
cce
ss a
nd
mo
dify d
ata
in
a
rela
tio
na
l d
ata
ba
se
Is
a n
on
pro
ce
du
ral la
ng
ua
ge
A
lso k
now
n a
s a
"decla
rative language,"
it allo
ws t
he
pro
gra
mm
er
to focus o
n input and o
utp
ut ra
ther
than the
pro
gra
m s
teps
Is
a 4
GL
(fo
urt
h-g
en
era
tio
n-p
rog
ram
min
g la
ng
ua
ge
)
A
la
ng
ua
ge
th
at is
clo
se
r to
natu
ral la
nguage t
han a
p
rog
ram
min
g la
ng
ua
ge
; q
ue
ry la
ng
ua
ge
s a
re g
en
era
lly 4
GL
Is
a c
om
mo
n q
ue
ry la
ng
ua
ge
fo
r m
an
y t
yp
es o
f d
ata
ba
se
s,
inclu
din
g O
racle
H
as b
ee
n s
tan
da
rdiz
ed
by th
e A
me
rica
n N
atio
na
l S
tan
da
rds
Institu
te (
AN
SI)
-
6
Intr
od
ucti
on
to
PL
/SQ
L
Copyright
2011,
Ora
cle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
SQ
L S
tate
men
t
Th
e S
QL
sta
tem
en
t sh
ow
n is s
imp
le a
nd
str
aig
htf
orw
ard
. H
ow
eve
r, if
yo
u w
an
t to
alte
r a
ny o
f th
e r
etr
ieve
d d
ata
in
a
co
nd
itio
na
l m
an
ne
r (if
the
da
ta is x
yz th
en
do
th
is to
it)
, yo
u
co
me
acro
ss t
he
lim
ita
tio
ns o
f S
QL
.
Fo
r e
xa
mp
le,
ho
w w
ou
ld y
ou
write
an
SQ
L s
tate
me
nt
to
up
da
te t
he
final_letter_grade
da
ta w
ith
va
ryin
g le
tte
r g
rad
es f
or
stu
de
nts
in
diffe
ren
t cla
sse
s?
SELECT class_id, stu_id,
final_numeric_grade, final_letter_grade
FROM enrollments;
-
7
Intr
od
ucti
on
to
PL
/SQ
L
Copyright
2011,
Ora
cle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Lim
ita
tio
ns
of
SQ
L
CL
AS
S_
IDS
TU
_ID
FIN
AL
_N
UM
ER
IC_
GR
AD
EF
INA
L_
LE
TT
ER
_G
RA
DE
1101
75
1107
71
1131
65
2155
91
2114
93
En
roll
me
nts
For class_id=1
If 66 3;
END LOOP;
END;
-
8
Itera
tive C
on
tro
l: B
asic
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ba
sic
Lo
op
s EXIT
Sta
tem
en
t
You c
an u
se the EXIT
sta
tem
en
t to
te
rmin
ate
a lo
op
. T
he
co
ntr
ol p
asse
s t
o t
he
ne
xt
sta
tem
en
t a
fte
r th
e END LOOP
sta
tem
ent. Y
ou c
an issue EXIT
either
as a
n a
ction w
ithin
an
IF
sta
tem
en
t o
r a
s a
sta
nd
-alo
ne
sta
tem
en
t w
ith
in t
he
lo
op
.
DECLARE
v_counter NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('The square of '
||v_counter||' is: '|| POWER(v_counter,2));
v_counter := v_counter + 1;
IF v_counter > 10 THEN
EXIT;
END IF;
END LOOP;
END;
-
9
Itera
tive C
on
tro
l: B
asic
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ba
sic
Lo
op
EXIT
Sta
tem
en
t R
ule
s
T
heEXIT
sta
tem
en
t m
ust
be
pla
ce
d in
sid
e a
lo
op
.
If the EXIT
conditio
n is p
laced a
t th
e t
op o
f th
e loop
(be
fore
an
y o
f th
e o
the
r e
xe
cuta
ble
sta
tem
ents
) and t
hat
conditio
n is initia
lly tru
e, th
en the loop e
xits a
nd the o
ther
sta
tem
en
ts in
th
e lo
op
ne
ve
r e
xe
cu
te.
A
basic
loop c
an c
onta
in m
ultip
le EXIT
sta
tem
en
ts,
bu
t
you s
hould
have o
nly
one EXIT
po
int.
-
10
Itera
tive C
on
tro
l: B
asic
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ba
sic
Lo
op
EXIT WHEN S
tate
men
t
Use the WHEN
cla
use t
o a
llow
conditio
nal te
rmin
ation o
f th
e loop.
When the EXIT
sta
tem
en
t is
en
co
un
tere
d, th
e c
on
ditio
n in
th
e
WHEN
cla
use is e
valu
ate
d.
If t
he c
onditio
n y
ield
s TRUE
, th
en
th
e
loo
p e
nd
s a
nd
co
ntr
ol p
asse
s t
o t
he
ne
xt
sta
tem
en
t a
fte
r th
e
loop.
DECLARE
v_counter NUMBER := 1;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('The square of '
||v_counter||' is: '|| POWER(v_counter,2));
v_counter := v_counter + 1;
EXIT WHEN v_counter > 10;
END LOOP;
END;
-
11
Itera
tive C
on
tro
l: B
asic
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Te
rmin
olo
gy
Key term
s u
sed in this
lesson inclu
de:
Ba
sic
(In
fin
ite
) lo
op
EX
IT
-
12
Itera
tive C
on
tro
l: B
asic
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Su
mm
ary
Ob
jecti
ves S
um
mari
zed
In t
his
lesson,
you learn
ed t
o:
D
escrib
e t
he
ne
ed
fo
r LOOP
sta
tem
ents
in P
L/S
QL
R
eco
gn
ize
diffe
ren
t ty
pe
s o
f LOOP
sta
tem
en
ts
C
reate
PL/S
QL c
onta
inin
g a
basic
loop a
nd a
n EXIT
sta
tem
en
t
C
reate
PL/S
QL c
onta
inin
g a
basic
loop a
nd a
n EXIT
sta
tem
en
t w
ith
co
nd
itio
na
l te
rmin
atio
n
-
13
Itera
tive C
on
tro
l: B
asic
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Try
It/
So
lve It
Exerc
ises
Th
e e
xe
rcis
es in
th
is le
sso
n c
ove
r th
e fo
llow
ing
to
pic
s:
D
escrib
ing
th
e n
ee
d f
or LOOP
sta
tem
ents
in P
L/S
QL
Id
en
tify
ing
diffe
ren
t ty
pe
s o
f LOOP
sta
tem
en
ts
U
sin
g b
asic
loops w
ith EXIT
conditio
ns
U
sin
g b
asic
loops w
ith EXIT WHEN c
onditio
ns
-
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Ite
rati
ve
Co
ntr
ol:
WHILE
an
dFOR
Lo
op
s
-
2
Itera
tive C
on
tro
l: WHILE
an
dFOR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
at
Will I L
earn
?
Ob
jec
tiv
es
In t
his
lesson,
you w
ill learn
to:
C
onstr
uct
and u
se t
he WHILE
loopin
g c
onstr
uct in
PL/S
QL
C
onstr
uct
and u
se t
he FOR
loopin
g c
onstr
uct in
PL/S
QL
D
escribe w
hen a
WHILE
loop is u
sed in P
L/S
QL
D
escribe w
hen a
FOR
loop is u
sed in P
L/S
QL
-
3
Itera
tive C
on
tro
l: WHILE
an
dFOR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
y L
earn
It?
Pu
rpo
se
The p
revio
us lesson d
iscussed t
he b
asic
loop,
whic
h r
equired
tha
t th
e s
tate
me
nts
in
sid
e t
he
lo
op
exe
cu
te a
t le
ast
on
ce
.
This
lesson intr
oduces the WHILE
loo
p a
nd
FOR
loop. T
he WHILE
loo
p is a
lo
op
ing
co
nstr
uct,
wh
ich
re
qu
ire
s t
ha
t th
e EXIT
co
nd
itio
n b
e e
va
lua
ted
at
the
sta
rt o
f e
ach
ite
ratio
n.
Th
e FOR
loo
p s
ho
uld
be
use
d if
the
nu
mb
er
of
ite
ratio
ns is k
no
wn
.
-
4
Itera
tive C
on
tro
l: WHILE
an
dFOR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
WHILE
Lo
op
s
You c
an u
se the WHILE
loo
p t
o r
ep
ea
t a
se
qu
en
ce
of
sta
tem
en
ts
until th
e c
ontr
olli
ng c
onditio
n is n
o longer TRUE
. T
he
co
nd
itio
n is
eva
lua
ted
at
the
sta
rt o
f e
ach
ite
ratio
n.
Th
e lo
op
te
rmin
ate
s w
he
n
the
co
nd
itio
n is FALSE
orNULL
. If
th
e c
on
ditio
n is FALSE
orNULL
at
the
sta
rt o
f th
e lo
op
, th
en
no
fu
rth
er
ite
ratio
ns a
re p
erf
orm
ed
.
WHILEconditionLOOP
statement1;
statement2;
. . .
END LOOP;
-
5
Itera
tive C
on
tro
l: WHILE
an
dFOR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
WHILE
Lo
op
s (
co
nti
nu
ed
)
In
th
e s
yn
tax:
condition
is a
Boole
an v
ariable
or
expre
ssio
n (TRUE
,
FALSE
, o
r NULL
)
statement
can b
e o
ne o
r m
ore
PL/S
QL o
r S
QL s
tate
ments
If t
he v
ariable
s involv
ed in t
he c
onditio
ns d
o n
ot
change
du
rin
g t
he
bo
dy o
f th
e lo
op
, th
en
th
e c
on
ditio
n r
em
ain
s TRUE
an
d t
he
lo
op
do
es n
ot
term
ina
te.
N
ote
:If t
he c
onditio
n y
ield
s NULL
, th
en t
he loop is b
ypassed
an
d t
he
co
ntr
ol p
asse
s t
o t
he
ne
xt
sta
tem
en
t.
WHILEconditionLOOP
statement1;
statement2;
. . .
END LOOP;
-
6
Itera
tive C
on
tro
l: WHILE
an
dFOR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
WHILE
Lo
op
s (
co
nti
nu
ed
)
In t
he
exa
mp
le in
th
e s
lide
, th
ree
ne
w lo
ca
tio
n I
Ds f
or
the
co
un
try c
od
e C
A a
nd
th
e c
ity o
f M
on
tre
al a
re b
ein
g a
dd
ed
.
The c
ounte
r is
explic
itly
decla
red in t
his
exam
ple
.
DECLARE
v_countryid locations.country_id%TYPE := 'CA';
v_loc_id locations.location_id%TYPE;
v_new_city locations.city%TYPE := 'Montreal';
v_counter NUMBER := 1;
BEGIN
SELECT MAX(location_id) INTO v_loc_id FROM locations
WHERE country_id = v_countryid;
WHILE v_counter 3;
LOOPDBMS_OUTPUT.PUT_LINE('Outer loop is:'||v_outerloop||
' and inner loop is: '||v_innerloop);
v_innerloop := v_innerloop - 1;
EXIT WHEN v_innerloop =0;
END LOOP Inner_loop;
END LOOP Outer_loop;
END;
-
9
Itera
tive C
on
tro
l: N
este
d L
oo
ps
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ne
ste
d L
oo
ps
an
d L
ab
els
In this
exam
ple
, th
ere
are
tw
o loop
s.
The o
ute
r lo
op is identified b
y
the label
, and t
he inner
loop is identified b
y t
he
lab
el
.
BEGIN
LOOP
v_counter := v_counter+1;
EXIT WHEN v_counter>10;
LOOP...
EXIT Outer_loop WHEN v_total_done = 'YES';
-- Leave both loops
EXIT WHEN v_inner_done = 'YES';
-- Leave inner loop only
...
END LOOP Inner_loop;
...
END LOOP Outer_loop;
END;
-
10
Itera
tive C
on
tro
l: N
este
d L
oo
ps
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Su
mm
ary
Ob
jecti
ves S
um
mari
zed
In t
his
lesson,
you learn
ed t
o:
C
onstr
uct and e
xecute
PL/S
QL u
sin
g n
este
d loops
L
ab
el lo
op
s a
nd
use
th
e la
be
ls
n E
XIT
sta
tem
ents
E
valu
ate
a n
este
d loop c
onstr
uct
and identify
the e
xit
poin
t
-
11
Itera
tive C
on
tro
l: N
este
d L
oo
ps
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Try
It/
So
lve It
Exerc
ises
Th
e e
xe
rcis
es in
th
is le
sso
n c
ove
r th
e fo
llow
ing
to
pic
s:
C
onstr
ucting a
nd e
xecuting P
L/S
QL u
sin
g n
este
d loops
Labelin
g loops a
nd u
sin
g the labels
n
EX
IT s
tate
me
nts
E
va
lua
tin
g a
ne
ste
d lo
op
co
nstr
uct
an
d id
en
tify
ing
th
e e
xit
poin
t
-
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Intr
od
ucti
on
to
Exp
licit
Cu
rso
rs
-
2
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
at
Will I L
earn
?
Ob
jec
tiv
es
In t
his
lesson,
you w
ill learn
to:
D
istinguis
h b
etw
een a
n im
plic
it a
nd a
n e
xplic
it c
urs
or
D
escribe w
hy a
nd w
hen t
o u
se a
n e
xplic
it c
urs
or
in P
L/S
QL
co
de
L
ist
two
or
mo
re g
uid
elin
es f
or
de
cla
rin
g a
nd
co
ntr
olli
ng
explic
it c
urs
ors
C
reate
PL/S
QL c
ode t
hat
successfu
lly o
pens a
curs
or
and
fetc
he
s a
pie
ce
of
da
ta in
to a
va
ria
ble
U
se a
sim
ple
loop t
o f
etc
h m
ultip
le r
ow
s f
rom
a c
urs
or
C
reate
PL/S
QL c
ode that successfu
lly c
loses a
curs
or
after
fetc
hin
g d
ata
in
to a
va
ria
ble
-
3
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
y L
earn
It?
Pu
rpo
se
Yo
u h
ave
le
arn
ed
th
at
an
SQ
L SELECT
sta
tem
en
t in
a P
L/S
QL
blo
ck is s
ucce
ssfu
l o
nly
if it r
etu
rns e
xa
ctly o
ne
ro
w.
What if y
ou n
eed to w
rite
a SELECT
sta
tem
en
t th
at
retu
rns m
ore
tha
n o
ne
ro
w?
F
or
exa
mp
le,
yo
u n
ee
d t
o p
rod
uce
a r
ep
ort
of
all
em
plo
ye
es?
To
re
turn
mo
re t
ha
n o
ne
ro
w,
yo
u m
ust
de
cla
re a
nd
use
an
explic
it c
urs
or.
-
4
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Co
nte
xt
Are
as
an
d C
urs
ors
Th
e O
racle
se
rve
r a
lloca
tes a
priva
te m
em
ory
are
a c
alle
d a
co
nte
xt
are
a t
o s
tore
th
e d
ata
pro
ce
sse
d b
y a
n S
QL
sta
tem
en
t.
Eve
ry c
on
text
are
a (
an
d t
he
refo
re e
ve
ry S
QL
sta
tem
en
t) h
as a
curs
or
associa
ted w
ith it.
You c
an
th
ink o
f a
cu
rso
r e
ith
er
as a
la
be
l fo
r th
e c
on
text
are
a,
or
as a
po
inte
r to
th
e c
on
text
are
a.
In
fact,
a c
urs
or
is b
oth
of
the
se
ite
ms.
Co
nte
xt
Are
a
Cu
rso
r
-
5
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Imp
lic
it a
nd
Ex
pli
cit
Cu
rso
rs
Th
ere
are
tw
o t
yp
es o
f cu
rso
rs:
Im
plic
it c
urs
ors
: D
efin
ed
au
tom
atica
lly b
y O
racle
fo
r a
ll S
QL
D
ML
sta
tem
en
ts (INSERT
,UPDATE
,DELETE
,an
d MERGE
),and f
or SELECT
sta
tem
en
ts t
ha
t re
turn
on
ly o
ne
ro
w.
E
xp
licit c
urs
ors
: D
ecla
red
by t
he
pro
gra
mm
er
for
qu
erie
s
tha
t re
turn
mo
re t
ha
n o
ne
ro
w.
Yo
u c
an
use
exp
licit c
urs
ors
to
na
me
a c
on
text
are
a a
nd
acce
ss its
sto
red
da
ta.
-
6
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Lim
ita
tio
ns
of
Imp
lic
it C
urs
ors
Th
ere
is m
ore
th
an
on
e r
ow
in
th
e EMPLOYEES
table
:
DECLARE
v_salary employees.salary%TYPE;
BEGIN
SELECT salary INTO v_salary
FROM employees;
DBMS_OUTPUT.PUT_LINE(' Salary is : '||v_salary);
END;
ORA-01422: exact fetch returns more than requested number of rows
-
7
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ex
pli
cit
Cu
rso
rs
With
an
exp
licit c
urs
or,
yo
u c
an
re
trie
ve
mu
ltip
le r
ow
s fro
m a
d
ata
ba
se
ta
ble
, h
ave
a p
oin
ter
to e
ach
ro
w t
ha
t is
re
trie
ve
d,
an
d w
ork
on
th
e r
ow
s o
ne
at
a t
ime
.
Th
e f
ollo
win
g a
re s
om
e r
ea
so
ns t
o u
se
an
exp
licit c
urs
or:
It
is t
he
on
ly w
ay in
PL
/SQ
L t
o r
etr
ieve
mo
re t
ha
n o
ne
ro
w f
rom
a t
ab
le.
E
ach
ro
w is f
etc
he
d b
y a
se
pa
rate
pro
gra
m s
tate
me
nt,
g
ivin
g t
he
pro
gra
mm
er
mo
re c
on
tro
l o
ve
r th
e p
roce
ssin
g
of
the r
ow
s.
-
8
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Exam
ple
of
an
Exp
licit
Cu
rso
r
Th
e f
ollo
win
g e
xa
mp
le u
se
s a
n e
xp
licit c
urs
or
to o
bta
in t
he
co
un
try
nam
e a
nd n
ational holid
ay for
countr
ies in A
sia
.
DECLARE
CURSOR wf_holiday_cursor IS
SELECT country_name, national_holiday_date
FROM wf_countries where region_id IN(30,34,35);
v_country_name wf_countries.country_name%TYPE;
v_holiday
wf_countries.national_holiday_date%TYPE;
BEGIN
OPEN wf_holiday_cursor;
LOOP
FETCH wf_holiday_cursor INTO v_country_name, v_holiday;
EXIT WHEN wf_holiday_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_country_name||' '||v_holiday);
END LOOP;
CLOSE wf_holiday_cursor;
END;
-
9
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ex
pli
cit
Cu
rso
r O
pe
rati
on
s
Th
e s
et
of
row
s r
etu
rne
d b
y a
mu
ltip
le-r
ow
qu
ery
is c
alle
d t
he
a
cti
ve
se
t, a
nd
is s
tore
d in
th
e c
on
text
are
a.
Its s
ize
is t
he
n
um
be
r o
f ro
ws t
ha
t m
ee
t yo
ur
se
arc
h c
rite
ria
.
Ac
tiv
e s
et
Tab
le
100 King AD_PRES
101 Kochhar AD_VP
102 De Haan AD_VP
139 Seo
ST_CLERK
Exp
licit
Cu
rso
r
-
10
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ex
pli
cit
Cu
rso
r O
pe
rati
on
s (
co
nti
nu
ed
)
Th
ink o
f th
e c
on
text
are
a (
na
me
d b
y t
he
cu
rso
r) a
s a
bo
x,
an
d
the
active
se
t a
s t
he
co
nte
nts
of
the
bo
x.
To
ge
t a
t th
e d
ata
, yo
u
mu
stOPEN
the
bo
x a
nd
FETCH
ea
ch
ro
w f
rom
th
e b
ox o
ne
at
a
tim
e.
When f
inis
hed,
you m
ust CLOSE
the
bo
x.
Ac
tiv
e s
et
Tab
le
100 King AD_PRES
101 Kochhar AD_VP
102 De Haan AD_VP
139 Seo
ST_CLERK
Exp
licit
Cu
rso
r
-
11
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Co
ntr
oll
ing
Ex
pli
cit
Cu
rso
rs
Fetc
h e
ach
ro
w,
on
e a
t a
tim
e. C
los
e t
he
cu
rso
r.
Cu
rso
r
po
inte
r
Op
en
th
e c
urs
or.
1
2
3
Cu
rso
r
po
inte
r
Cu
rso
r
po
inte
r
-
12
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
De
cla
rin
g a
nd
Co
ntr
oll
ing
Ex
pli
cit
Cu
rso
rs
R
etr
ieve
the
cu
rre
nt
row
in
to
va
ria
ble
s.
FETCH
T
es
t fo
r
exis
tin
g
row
s.
EMPTY?
R
etu
rn t
o
FETCH
if
row
s a
re
fou
nd
.
No
R
ele
as
e t
he
ac
tiv
e s
et.
CLOSE
Ye
s
N
am
e a
n
ac
tiv
e s
et.
DECLARE
F
ill th
e
ac
tiv
e s
et
wit
h d
ata
.
OPEN
-
13
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ste
ps
fo
r U
sin
g E
xp
lic
it C
urs
ors
You first D
EC
LA
RE
a c
urs
or,
and then y
ou u
se the O
PE
N,
FE
TC
H, a
nd
CL
OS
E s
tate
me
nts
to
co
ntr
ol a
cu
rso
r.
1.D
EC
LA
RE
the c
urs
or
in t
he d
ecla
rative s
ection.
2.O
PE
N the c
urs
or.
3.F
ET
CH
each r
ow
fro
m t
he a
ctive s
et
until th
e b
ox is e
mpty
.
4.C
LO
SE
the c
urs
or.
The O
PE
N s
tate
ment
execute
s t
he q
uery
associa
ted w
ith the
cu
rso
r, id
en
tifie
s t
he
re
su
lt s
et,
an
d p
ositio
ns t
he
cu
rso
r b
efo
re
the
first
row
. T
he
FE
TC
H s
tate
me
nt
retr
ieve
s t
he
cu
rre
nt
row
a
nd
ad
va
nce
s t
he
cu
rso
r to
th
e n
ext
row
. W
he
n t
he
la
st
row
ha
s
been p
rocessed,
the C
LO
SE
sta
tem
ent
dis
able
s t
he c
urs
or.
-
14
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Sy
nta
x f
or
De
cla
rin
g t
he
Cu
rso
r
Th
e a
ctive
se
t o
f a
cu
rso
r is
de
term
ine
d b
y t
he
SELECT
sta
tem
en
t in
th
e c
urs
or
de
cla
ratio
n.
Synta
x: In
the s
ynta
x:
cursor_name
Is a
PL/S
QL identifier
select_statement
Is a
SELECT
sta
tem
ent
without
an
INTO
cla
use
CURSORcursor_nameIS
select_statement;
-
15
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Decla
rin
g t
he C
urs
or
Exam
ple
1
Theemp_cursor
cu
rso
r is
de
cla
red
to
re
trie
ve
th
e
employee_id
an
dlast_name
co
lum
ns o
f th
e e
mp
loye
es
wo
rkin
g in
th
e d
ep
art
me
nt
with
a department_id
of 30.
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees
WHERE department_id =30;
...
-
16
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Decla
rin
g t
he C
urs
or
Exam
ple
2T
hedept_cursor
cu
rso
r is
de
cla
red
to
re
trie
ve
all
the
de
tails
fo
r th
e d
ep
art
me
nts
with
th
e location_id
17
00
. Y
ou
wa
nt
to
fetc
h a
nd
pro
ce
ss t
he
se
ro
ws in
asce
nd
ing
se
qu
en
ce
by
department_name
.
DECLARE
CURSOR dept_cursor IS
SELECT * FROM departments
WHERE location_id = 1700
ORDER BY department_name;
...
-
17
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Decla
rin
g t
he C
urs
or
Exam
ple
3
A S
ELE
CT
sta
tem
ent
in a
curs
or
decla
ration c
an inclu
de join
s,
gro
up
fu
nctio
ns,
an
d s
ub
qu
erie
s.
Th
is e
xa
mp
le r
etr
ieve
s e
ach
d
ep
art
me
nt
tha
t h
as a
t le
ast
two
em
plo
ye
es,
giv
ing
th
e
de
pa
rtm
en
t n
am
e a
nd
nu
mb
er
of
em
plo
ye
es.
DECLARE
CURSOR dept_emp_cursor IS
SELECT department_name, COUNT(*) AS how_many
FROM departments d, employees e
WHERE d.department_id = e.department_id
GROUP BY d.department_name
HAVING COUNT(*) > 1;
...
-
18
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Gu
ide
lin
es
fo
r D
ec
lari
ng
th
e C
urs
or
D
o n
ot
inclu
de t
he INTO
cla
use
in
th
e c
urs
or
de
cla
ratio
n
be
ca
use
it
ap
pe
ars
la
ter
in t
he
FETCH
sta
tem
en
t.
If
pro
ce
ssin
g r
ow
s in
a s
pe
cific
se
qu
en
ce
is r
eq
uire
d, th
en
use t
he ORDER BY c
lause in t
he q
uery
.
T
he
cu
rso
r ca
n b
e a
ny v
alid
SELECT
sta
tem
en
t, in
clu
din
g
join
s, subqueries, and s
o o
n.
If
a c
urs
or
de
cla
ratio
n r
efe
ren
ce
s a
ny P
L/S
QL
va
ria
ble
s,
the
se
va
ria
ble
s m
ust
be
de
cla
red
be
fore
de
cla
rin
g t
he
cu
rso
r.
-
19
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Op
en
ing
th
e C
urs
or
TheOPEN
sta
tem
en
t e
xe
cu
tes t
he
qu
ery
asso
cia
ted
with
th
e
curs
or,
identifies t
he a
ctive s
et, a
nd p
ositio
ns t
he c
urs
or
po
inte
r to
th
e f
irst
row
. T
he
OPEN
sta
tem
en
t is
in
clu
de
d in
the e
xecuta
ble
section o
f th
e P
L/S
QL b
lock.
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees
WHERE department_id = 30;
...
BEGIN
OPEN emp_cursor;
...
-
20
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Op
en
ing
th
e C
urs
or
(co
nti
nu
ed
)
TheOPEN
sta
tem
en
t p
erf
orm
s t
he
fo
llow
ing
op
era
tio
ns:
1.
Allo
ca
tes m
em
ory
fo
r a
co
nte
xt
are
a (
cre
ate
s t
he
bo
x)
2.
Execute
s the SELECT
sta
tem
en
t in
th
e c
urs
or
de
cla
ratio
n,
retu
rnin
g t
he
re
su
lts in
to t
he
active
se
t (f
ills
the
bo
x w
ith
da
ta)
3.
Po
sitio
ns t
he
po
inte
r to
th
e f
irst
row
in
th
e a
ctive
se
t
-
21
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Fe
tch
ing
Da
ta f
rom
th
e C
urs
or
TheFETCH
sta
tem
en
t re
trie
ve
s t
he
ro
ws f
rom
th
e c
urs
or
on
e a
t a
tim
e.
Aft
er
ea
ch
fe
tch
, th
e c
urs
or
ad
va
nce
s t
o t
he
ne
xt
row
in
th
e
active
se
t. T
wo
va
ria
ble
s, v
_empno
an
dv_lname
, a
re d
ecla
red
to
ho
ld t
he
fe
tch
ed
va
lue
s f
rom
th
e c
urs
or.
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees
WHERE department_id =10;
v_empno employees.employee_id%TYPE;
v_lname employees.last_name%TYPE;
BEGIN
OPEN emp_cursor;
FETCH emp_cursor INTO v_empno, v_lname;
DBMS_OUTPUT.PUT_LINE( v_empno ||' '||v_lname);
...
END;
200 Whalen
Statement processed.
-
22
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Fe
tch
ing
Da
ta f
rom
th
e C
urs
or
(co
nti
nu
ed
)
You h
ave s
uccessfu
lly f
etc
hed t
he v
alu
es f
rom
the c
urs
or
into
th
e v
aria
ble
s.
Ho
we
ve
r, t
he
re a
re s
ix e
mp
loye
es in
d
ep
art
me
nt
30
. O
nly
on
e r
ow
ha
s b
ee
n f
etc
he
d.
To
fe
tch
all
the
ro
ws, yo
u h
ave
to
ma
ke
use
of lo
op
s.
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees
WHERE department_id =50;
v_empno employees.employee_id%TYPE;
v_lname employees.last_name%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_empno, v_lname;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( v_empno ||' '||v_lname);
END LOOP;
END;
124 Mourgos
141 Rajs
142 Davies
143 Matos
144 Vargas
Statement processed.
-
23
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Gu
ide
lin
es
fo
r F
etc
hin
g D
ata
Fro
m t
he
Cu
rso
r
In
clu
de
th
e s
am
e n
um
be
r o
f va
ria
ble
s in
th
e INTO
cla
use
of th
e FETCH
sta
tem
en
t a
s c
olu
mn
s in
th
e SELECT
sta
tem
en
t, a
nd
be
su
re t
ha
t th
e d
ata
typ
es a
re c
om
pa
tib
le.
M
atc
h e
ach
va
ria
ble
to
co
rre
sp
on
d t
o t
he
co
lum
ns
positio
nally
.
T
est
to s
ee
wh
eth
er
the
cu
rso
r co
nta
ins r
ow
s.
If a
fe
tch
a
cq
uire
s n
o v
alu
es,
the
n t
he
re a
re n
o r
ow
s le
ft t
o p
roce
ss
in t
he
active
se
t a
nd
no
err
or
is r
eco
rde
d.
Th
e la
st
row
is
re-p
roce
sse
d.
Y
ou c
an u
se the %NOTFOUND
cu
rso
r a
ttrib
ute
to
te
st
for
the
exit c
onditio
n.
-
24
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Fe
tch
ing
Da
ta F
rom
th
e C
urs
or
Ex
am
ple
1
What is
wro
ng w
ith this
exam
ple
?
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name, salary FROM employees
WHERE department_id =30;
v_empno employees.employee_id%TYPE;
v_lname employees.last_name%TYPE;
v_sal employees.salary%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_empno, v_lname;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( v_empno ||' '||v_lname);
END LOOP;
END;
-
25
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Fe
tch
ing
Da
ta F
rom
th
e C
urs
or
Ex
am
ple
2
Th
ere
is o
nly
on
e e
mp
loye
e in
de
pa
rtm
en
t 1
0.
Wh
at
happens w
hen this
exam
ple
is e
xecute
d?
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees
WHERE department_id =10;
v_empno employees.employee_id%TYPE;
v_lname employees.last_name%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_empno, v_lname;
DBMS_OUTPUT.PUT_LINE( v_empno ||' '||v_lname);
END LOOP;
END;
-
26
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Clo
sin
g t
he
Cu
rso
rT
heCLOSE
sta
tem
en
t d
isa
ble
s t
he
cu
rso
r, r
ele
ase
s t
he
co
nte
xt
are
a,
an
d u
nd
efin
es t
he
active
se
t. C
lose
th
e c
urs
or
aft
er
com
ple
ting t
he p
rocessin
g o
f th
e FETCH
sta
tem
ent. Y
ou c
an
reo
pe
n t
he
cu
rso
r la
ter
if r
eq
uire
d.
Th
ink o
f CLOSE
as c
losin
g a
nd
em
pty
ing
th
e b
ox, so
yo
u c
an
no
lo
ng
erFETCH
its c
on
ten
ts.
... LOOP
FETCH emp_cursor INTO v_empno, v_lname;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE( v_empno ||' '||v_lname);
END LOOP;
CLOSE emp_cursor;
END;
-
27
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Gu
ide
lin
es
fo
r C
los
ing
th
e C
urs
or
A
cu
rso
r ca
n b
e r
eo
pe
ne
d o
nly
if
it is c
lose
d.
If y
ou
att
em
pt
to f
etc
h d
ata
fro
m a
cu
rso
r a
fte
r it h
as b
ee
n c
lose
d,
the
n a
n
INVALID_CURSOR
exce
ptio
n is r
ais
ed
.
If y
ou late
r re
open t
he c
urs
or,
the a
ssocia
ted S
ELE
CT
sta
tem
en
t is
re
-exe
cu
ted
to
re
-po
pu
late
th
e c
on
text
are
a
with
th
e m
ost
rece
nt
da
ta f
rom
th
e d
ata
ba
se
.
-
28
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Pu
ttin
g I
t A
ll T
og
eth
er
Th
e f
ollo
win
g e
xa
mp
le d
ecla
res a
nd
pro
ce
sse
s a
cu
rso
r to
ob
tain
th
e c
ountr
y n
am
e a
nd n
ational holid
ay f
or
countr
ies in A
sia
.
DECLARE
CURSOR wf_holiday_cursor IS
SELECT country_name, national_holiday_date
FROM wf_countries where region_id IN(30,34,35);
v_country_name wf_countries.country_name%TYPE;
v_holiday
wf_countries.national_holiday_date%TYPE;
BEGIN
OPEN wf_holiday_cursor;
LOOP
FETCH wf_holiday_cursor INTO v_country_name, v_holiday;
EXIT WHEN wf_holiday_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_country_name||' '||v_holiday);
END LOOP;
CLOSE wf_holiday_cursor;
END;
-
29
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Te
rmin
olo
gy
Key term
s u
sed in this
lesson inclu
de:
Co
nte
xt
are
a
Curs
or
Implic
it c
urs
or
Explic
it c
urs
or
Active s
et
FETCH
OPEN
CLOSE
-
30
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Su
mm
ary
Ob
jecti
ves S
um
mari
zed
In t
his
lesson,
you learn
ed t
o:
D
istinguis
h b
etw
een a
n im
plic
it a
nd a
n e
xplic
it c
urs
or
D
escribe w
hy a
nd w
hen t
o u
se a
n e
xplic
it c
urs
or
in P
L/S
QL
co
de
L
ist
two
or
mo
re g
uid
elin
es f
or
de
cla
rin
g a
nd
co
ntr
olli
ng
explic
it c
urs
ors
C
reate
PL/S
QL c
ode t
hat
successfu
lly o
pens a
curs
or
and
fetc
he
s a
pie
ce
of
da
ta in
to a
va
ria
ble
U
se a
sim
ple
loop t
o f
etc
h m
ultip
le r
ow
s f
rom
a c
urs
or
C
reate
PL/S
QL c
ode that successfu
lly c
loses a
curs
or
after
fetc
hin
g d
ata
in
to a
va
ria
ble
-
31
Intr
od
uc
tio
n t
o E
xp
lic
it C
urs
ors
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Try
It/
So
lve It
Exerc
ises
Th
e e
xe
rcis
es in
th
is le
sso
n c
ove
r th
e fo
llow
ing
to
pic
s:
D
istinguis
hin
g b
etw
een a
n im
plic
it a
nd a
n e
xplic
it c
urs
or
D
iscussin
g w
hen a
nd w
hy to u
se a
n e
xplic
it c
urs
or
D
ecla
rin
g a
nd
co
ntr
olli
ng
exp
licit c
urs
ors
U
sin
g a
sim
ple
lo
op
to
fe
tch
mu
ltip
le r
ow
s f
rom
a c
urs
or
-
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Usin
g E
xp
licit
Cu
rso
r A
ttri
bu
tes
-
2
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
at
Will I L
earn
?
Ob
jec
tiv
es
In t
his
lesson,
you w
ill learn
to:
D
efin
e a
re
co
rd s
tru
ctu
re u
sin
g t
he
%ROWTYPE
att
rib
ute
C
reate
PL/S
QL c
ode t
o p
rocess t
he r
ow
s o
f an a
ctive s
et
usin
g r
eco
rd typ
es in
cu
rso
rs
R
etr
ieve
in
form
atio
n a
bo
ut th
e s
tate
of a
n e
xp
licit c
urs
or
usin
g c
urs
or
att
rib
ute
s
-
3
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
y L
earn
It?
Pu
rpo
se
On
e o
f th
e r
ea
so
ns t
o u
se
exp
licit c
urs
ors
is t
ha
t th
ey g
ive
yo
u
gre
ate
r p
rog
ram
ma
tic c
on
tro
l w
he
n h
an
dlin
g y
ou
r d
ata
. T
his
le
sson d
iscusses techniq
ues for
usin
g e
xplic
it c
urs
ors
more
e
ffe
ctive
ly.
C
urs
or
reco
rds e
na
ble
yo
u t
o d
ecla
re a
sin
gle
va
ria
ble
fo
r all
the s
ele
cte
d c
olu
mns in a
curs
or.
C
urs
or
att
rib
ute
s e
na
ble
yo
u to
re
trie
ve
in
form
atio
n a
bo
ut
the
sta
te o
f yo
ur
exp
licit c
urs
or.
-
4
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rs a
nd
Reco
rds
The c
urs
or
in this
exam
ple
is b
ased o
n a
SE
LE
CT
sta
tem
ent
tha
t re
trie
ve
s o
nly
tw
o c
olu
mn
s o
f e
ach
ta
ble
ro
w:
Wh
at
if it
retr
ieve
d s
ix c
olu
mn
s .
. o
r se
ve
n,
or
eig
ht,
or
twe
nty
?
DECLARE
v_emp_id
employees.employee_id%TYPE;
v_last_name employees.last_name%TYPE;
CURSOR emp_cursor IS
SELECT employee_id, last_name
FROM employees
WHERE department_id = 30;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor
INTO v_emp_id, v_last_name;
...
-
5
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rs a
nd
Re
co
rds
(c
on
tin
ue
d)
This
curs
or
retr
ieves w
hole
row
s o
f E
MP
LO
YE
ES
:
Me
ssy a
nd
lo
ng
-win
de
d,
isn
t it?
DECLARE
v_emp_id
employees.employee_id%TYPE;
v_first_name
employees.first_name%TYPE;
v_last_name
employees.last_name%TYPE;
...
v_department_id employees.department_id%TYPE;
CURSOR emp_cursor IS
SELECT * FROM employees
WHERE department_id = 30;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor
INTO v_emp_id, v_first_name, v_last_name ...
v_department_id;
...
-
6
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rs a
nd
Re
co
rds
(c
on
tin
ue
d)
Co
mp
are
th
e fo
llow
ing
sn
ipp
ets
of co
de
. W
ha
t d
iffe
ren
ce
s d
o y
ou
se
e? DECLARE
v_emp_id
...;
v_first_name
...;
...
v_department_id ...:
CURSOR emp_cursor IS
SELECT * FROM employees
WHERE department_id =30;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor
INTO v_emp_id,
v_first_name,
... v_department_id;
...
DECLARE
CURSOR emp_cursor IS
SELECT * FROM employees
WHERE department_id = 30;
v_emp_record
emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor
INTO v_emp_record;
...
-
7
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rs a
nd
Re
co
rds
(c
on
tin
ue
d)
Th
e c
od
e o
n t
he
rig
ht
use
s %ROWTYPE
to d
ecla
re a
re
co
rd
str
uctu
re b
ase
d o
n t
he
cu
rso
r. A
re
co
rd is a
co
mp
osite
da
ta
type in P
L/S
QL.
DECLARE
CURSOR emp_cursor IS
SELECT * FROM employees
WHERE department_id = 30;
v_emp_record
emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor
INTO v_emp_record;
...
Vari
ab
les
Reco
rds
DECLARE
v_emp_id
...;
v_first_name
...;
...
v_department_id ...:
CURSOR emp_cursor IS
SELECT * FROM employees
WHERE department_id = 30;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor
INTO v_emp_id,
v_first_name,
... v_department_id;
...
-
8
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Str
uc
ture
of
a P
L/S
QL
Re
co
rd
A r
eco
rd is a
co
mp
osite
da
ta t
yp
e,
co
nsis
tin
g o
f a
nu
mb
er
of
fie
lds e
ach
with
th
eir o
wn
na
me
an
d d
ata
typ
e.
Yo
u r
efe
ren
ce
ea
ch
fie
ld b
y d
ot-
pre
fixin
g its
fie
ld-n
am
e w
ith
th
e r
eco
rd-n
am
e.
%ROWTYPE
de
cla
res a
re
co
rd w
ith
th
e s
am
e f
ield
s a
s t
he
cu
rso
r o
n w
hic
h it is
ba
se
d.Fie
ld2
(d
ata
ty
pe
)F
ield
3 (
da
ta t
yp
e)
Fie
ld1
(d
ata
ty
pe
)
-
9
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Str
uc
ture
of cursor_name%ROWTYPE
v_em
p_re
co
rd.last_
nam
ev_em
p_re
co
rd.s
ala
ryv_em
p_re
co
rd.e
mp
loyee_id
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name, salary FROM employees
WHERE department_id = 30;
v_emp_record emp_cursor%ROWTYPE;
...
100
Kin
g24000
-
10
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rs a
nd
%ROWTYPE
%ROWTYPE
is c
onvenie
nt
for
pro
cessin
g t
he r
ow
s o
f th
e a
ctive
set
because y
ou c
an s
imply
fetc
h into
the r
ecord
.
DECLARE
CURSOR emp_cursor IS
SELECT * FROM employees
WHERE department_id = 30;
v_emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_record.employee_id|| ' - '
||v_emp_record.last_name);
END LOOP;
CLOSE emp_cursor;
END;
-
11
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rs a
nd
%ROWTYPE
: A
no
the
r E
xa
mp
le
DECLARE
CURSOR emp_dept_cursor IS
SELECT first_name, last_name, department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;
v_emp_dept_record
emp_dept_cursor%ROWTYPE;
BEGIN
OPEN emp_dept_cursor;
LOOP
FETCH emp_dept_cursor INTO v_emp_dept_record;
EXIT WHEN emp_dept_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_dept_record.first_name
||' '||v_emp_dept_record.last_name
||' '||v_emp_dept_record.department_name);
END LOOP;
CLOSE emp_dept_cursor;
END;
-
12
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
As w
ith
im
plic
it c
urs
ors
, th
ere
are
se
ve
ral a
ttrib
ute
s f
or
ob
tain
ing
sta
tus in
form
atio
n a
bo
ut
an
exp
licit c
urs
or.
Wh
en
a
pp
en
de
d t
o t
he
cu
rso
r va
ria
ble
na
me
, th
ese
att
rib
ute
s r
etu
rn
use
ful in
form
atio
n a
bo
ut
the
exe
cu
tio
n o
f a
cu
rso
r m
an
ipu
latio
n s
tate
me
nt.
Evalu
ate
s t
o t
he t
ota
l n
um
be
r o
f ro
ws
FETCH
ed
so
fa
r
Evalu
ate
s to TRUE
if t
he
mo
st
rece
nt
fetc
h
retu
rned a
row
; opposite o
f %NOTFOUND
Evalu
ate
s to TRUE
if t
he
mo
st
rece
nt
fetc
h
did
no
t re
turn
a r
ow
Evalu
ate
s to TRUE
if t
he c
urs
or
is o
pen
De
scri
pti
on
Boole
an
%FOUND
Num
ber
%ROWCOUNT
Boole
an
%NOTFOUND
Boole
an
%ISOPEN
Typ
eA
ttri
bu
te
-
13
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
%ISOPEN
Att
rib
ute
Yo
u c
an
fe
tch
ro
ws o
nly
wh
en
th
e c
urs
or
is
open.
Use t
he %ISOPEN
cu
rso
r a
ttrib
ute
be
fore
pe
rfo
rmin
g a
fe
tch
to
te
st
wh
eth
er
the
cu
rso
r is
open.%ISOPEN
retu
rns t
he
sta
tus o
f th
e
curs
or:
TR
UE
if open a
nd F
ALS
E if not.
Exam
ple
:
IF NOT emp_cursor%ISOPEN THEN
OPEN emp_cursor;
END IF;
LOOP
FETCH emp_cursor...
-
14
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
%ROWCOUNT
an
d%NOTFOUND
Att
rib
ute
s
Usually
the %ROWCOUNT
an
d%NOTFOUND
att
rib
ute
s a
re u
se
d in
a lo
op
to
de
term
ine
wh
en
to
exit t
he
lo
op
.
Use the %ROWCOUNT
cu
rso
r a
ttrib
ute
fo
r th
e f
ollo
win
g:
T
o p
roce
ss a
n e
xa
ct
nu
mb
er
of
row
s
To
co
un
t th
e n
um
be
r o
f ro
ws f
etc
he
d s
o f
ar
in a
lo
op
a
nd
/or
de
term
ine
wh
en
to
exit t
he
lo
op
Use the %NOTFOUND
cu
rso
r a
ttrib
ute
fo
r th
e f
ollo
win
g:
T
o d
ete
rmin
e w
he
the
r th
e q
ue
ry f
ou
nd
an
y r
ow
s m
atc
hin
g
yo
ur
crite
ria
T
o d
ete
rmin
e w
he
n t
o e
xit t
he
lo
op
-
15
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Exam
ple
of %ROWCOUNT
an
d%NOTFOUND
This
exam
ple
show
s h
ow
you c
an u
se %ROWCOUNT
and
%NOTFOUND
att
rib
ute
s f
or
exit c
on
ditio
ns in
a lo
op
.
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees;
v_emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_emp_record;
EXIT WHEN emp_cursor%ROWCOUNT > 10 OR emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_record.employee_id
||' '|| v_emp_record.last_name);
END LOOP;
CLOSE emp_cursor;
END;
-
16
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
in
SQ
L S
tate
me
nts
You c
annot
use a
n e
xplic
it c
urs
or
att
rib
ute
dire
ctly in
an
SQ
L
sta
tem
en
t. T
he
fo
llow
ing
co
de
re
turn
s a
n e
rro
r:
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, salary FROM employees
ORDER BY SALARY DESC;
v_emp_record emp_cursor%ROWTYPE;
v_count
NUMBER;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
INSERT INTO top_paid_emps
(employee_id, rank, salary)
VALUES
(v_emp_record.employee_id, emp_cursor%ROWCOUNT,
v_emp_record.salary);
...
-
17
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Te
rmin
olo
gy
Key term
s u
sed in this
lesson inclu
de:
Record
%ROWTYPE
%ISOPEN
%ROWCOUNT
%NOTFOUND
-
18
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Su
mm
ary
Ob
jecti
ves S
um
mari
zed
In t
his
lesson,
you learn
ed t
o:
D
efin
e a
re
co
rd s
tru
ctu
re u
sin
g t
he
%ROWTYPE
att
rib
ute
C
reate
PL/S
QL c
ode t
o p
rocess t
he r
ow
s o
f an a
ctive s
et
usin
g r
eco
rd typ
es in
cu
rso
rs
R
etr
ieve
in
form
atio
n a
bo
ut th
e s
tate
of a
n e
xp
licit c
urs
or
usin
g c
urs
or
att
rib
ute
s
-
19
Us
ing
Ex
pli
cit
Cu
rso
r A
ttri
bu
tes
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Try
It/
So
lve It
Exerc
ises
The e
xerc
ises in this
lesson c
over
the follo
win
g topic
s:
D
efin
ing
a r
eco
rd s
tru
ctu
re u
sin
g t
he
%ROWTYPE
att
rib
ute
C
reating P
L/S
QL c
ode t
o p
rocess t
he r
ow
s o
f an a
ctive s
et
usin
g r
eco
rd typ
es in
cu
rso
rs
R
etr
ievin
g in
form
atio
n a
bo
ut
the
sta
te o
f a
n e
xp
licit c
urs
or
usin
g c
urs
or
att
rib
ute
s
-
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Cu
rso
rFOR
Lo
op
s
-
2
Cu
rso
r F
OR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
at
Will I L
earn
?
Ob
jec
tiv
es
In t
his
lesson,
you w
ill learn
to:
L
ist a
nd
exp
lain
th
e b
en
efits
of u
sin
g c
urs
or FOR
loops
C
rea
te P
L/S
QL
co
de
to
de
cla
re a
cu
rso
r a
nd
ma
nip
ula
te
it in
a FOR
loo
p
C
reate
PL/S
QL c
ode c
onta
inin
g a
curs
or FOR
loo
p u
sin
g
a s
ubquery
-
3
Cu
rso
r F
OR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Wh
y L
earn
It?
Pu
rpo
se
Yo
u h
ave
alre
ad
y le
arn
ed
ho
w t
o d
ecla
re a
nd
use
a s
imp
le
explic
it c
urs
or,
usin
g DECLARE
,OPEN
, a
nd FETCH
in a
lo
op
, te
stin
g f
or %NOTFOUND
, a
nd
CLOSE
sta
tem
en
ts.
Would
nt it be e
asie
r if y
ou c
ould
do a
ll th
is w
ith just one
sta
tem
en
t?
Yo
u c
an
do
all
of th
is u
sin
g a
cu
rso
r FOR
loop.
-
4
Cu
rso
r F
OR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rFOR
Lo
op
s
A c
urs
or FOR
loo
p p
roce
sse
s r
ow
s in
an
exp
licit c
urs
or.
It is a
sh
ort
cu
t b
eca
use
th
e c
urs
or
is o
pe
ne
d,
a r
ow
is f
etc
he
d
on
ce
fo
r e
ach
ite
ratio
n in
th
e lo
op
, th
e lo
op
exits w
he
n th
e la
st
row
is p
roce
sse
d, a
nd
th
e c
urs
or
is c
lose
d a
uto
ma
tica
lly. T
he
lo
op its
elf is t
erm
inate
d a
uto
matically
at
the e
nd o
f th
e ite
ration
wh
en
th
e la
st
row
ha
s b
ee
n fe
tch
ed
.
Synta
x:
FORrecord_nameINcursor_nameLOOP
statement1;
statement2;
. . .
END LOOP;
-
5
Cu
rso
r F
OR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rFOR
Lo
op
s (
co
nti
nu
ed
)
In t
he
syn
tax:
record_name
Is t
he n
am
e o
f th
e im
plic
itly
decla
red
reco
rd (
as cursor_name%ROWTYPE
)
cursor_name
Is a
PL
/SQ
L id
en
tifie
r fo
r th
e p
revio
usly
de
cla
red
cu
rso
r
FORrecord_nameINcursor_nameLOOP
statement1;
statement2;
. . .
END LOOP;
-
6
Cu
rso
r F
OR
Lo
op
s
Copyright
2011, O
racle
. A
ll rights
reserv
ed.
Tell M
e/S
ho
w M
e
Cu
rso
rFOR
Lo
op
s
Note
:v_emp_record
is th
e r
eco
rd th
at
is im
plic
itly
de
cla
red
. Y
ou
can a
ccess t
he f
etc
hed d
ata
with t
his
im
plic
it r
ecord
as s
how
n in
the
slid
e.
No
va
ria
ble
s a
re d
ecla
red
to
ho
ld t
he
fe
tch
ed
da
ta b
y
usin
g th
e INTO
cla
use
. T
he
co
de
do
es n
ot
ha
ve
OPEN
an
dCLOSE
sta
tem
en
ts t
o o
pe
n a
nd
clo
se
th
e c
urs
or
resp
ective
ly.
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name FROM employees
WHERE department_id = 50;
BEGIN
FOR v_emp_record IN emp_cursor
LOOPDBMS_OUTPUT.PUT_LINE(v_emp_record.employee_id
|| ' ' ||v_emp_record.last_name);
END LOOP;
END;
-
7
Cu
rso
r F
OR
Lo
op
s
Copyright
2011, O
ra