Sie sind auf Seite 1von 4

PL/SQL Day 3

Subprograms and Packages


Subprograms (Procedures and Functions)
Practice 1
Create a procedure called USER_QUERY_EMP that accepts three parameters Parameter
p_mye!o is o" #$ parameter mode %hich pro&ides the emp!o &alue 'he other t%o
parameters p_my(o) a!d p_mysal are o" *U' mode 'he procedure retrie&es the salary
a!d (o) o" a! employee %ith the pro&ided employee !um)er a!d assi+!s those to the
t%o *U' parameters respecti&ely 'he procedure should ha!dle the error i" the emp!o
does !ot e,ist i! the EMP ta)le )y displayi!+ a! appropriate messa+e Use )i!d &aria)les
"or the t%o *U' Parameters
Compile the code- i!&o.e the procedure- a!d display the salary a!d (o) title "or
employee !um)er /031 Do the same "or employee !um)er /123
Practice 2
Create a "u!ctio! !amed USER_3$$U3L_C*MP that has three parameters p_e!o- p_sal
a!d p_comm "or passi!+ o! the &alues o" a! employee !um)er- the curre!t salary a!d
commissio! o" the employee respecti&ely 'he "u!ctio! calculates a!d retur!s the a!!ual
compe!satio! o" the employee )y usi!+ the "ollo%i!+ "ormula
a!!ual_compe!satio! 4 5p_sal6p_comm7812
#" the salary or commissio! &alue is $ULL the! 9ero should )e su)stituted "or it
:i&e a call to USER_3$$U3L_C*MP "rom a SELEC' stateme!t- a+ai!st the EMP ta)le
Practice 3
Create a "u!ctio! !amed USER_;3L#D_DEP'$* that has a si!+le parameter p_d!o to
accept a departme!t !um)er a!d retur!s a <**LE3$ &alue 'he "u!ctio! retur!s 'RUE i"
the departme!t !um)er e,ists i! the DEP' ta)le else it retur!s =3LSE
Create a procedure !amed S>*?_S'RE$:'> that accepts departme!t !um)er i! a
si!+le parameter p_dept!o "rom user 'he procedure +i&es a call to
USER_;3L#D_DEP'$* #" the "u!ctio! retur!s 'RUE the! the procedure "i!ds out ho%
ma!y employees are there i! the departme!t "rom the EMP ta)le a!d displays the same
o! the scree! #" the "u!ctio! retur!s =3LSE the! the procedure displays a! appropriate
error messa+e
:i&e call to S>*?_S'RE$:'> )y passi!+ o! departme!t !um)er 1@ Do the same "or
departme!t !um)er /A
Practice B
Create a procedure !amed 3DD_EMPL*YEE to hire a! employee Parameters to the
procedure are (o)- m+r- hiredate- salary- commissio! a!d dept!o ;alidate the "ollo%i!+C
a Employee !um)er is !ot ta.e! as a parameter )ut is auto +e!erated )y usi!+ a
SEQUE$CE
) Do) is either ECLERFG or E3$3LYS'G or ES3LESM3$G 'he i!put &alue ca! )e e!tered i!
a!y case 5upper or lo%er or i!itcap7
c M+r is a! e,isti!+ employee
d >iredate is less tha! system date
1/B
PL/SQL Day 3
e Salary must )e +reater tha! 0@@
" Commissio! is !ot !ull i" the (o) is S3LESM3$ =or a!y other (o)- commissio! is !ull
+ Dept!o must e,ist i! the DEP' ta)le
#!sert the record i" the a)o&e &alidatio!s are met a!d display a messa+e E1 ro%
i!sertedG #" the ro% is !ot i!serted +e!erate a! e,ceptio! a!d ha!dle it )y displayi!+ a!
appropriate messa+e
:i&e a call to 3DD_EMPL*YEE throu+h a! a!o!ymous PL/SQL )loc.
Practice H
Create a "u!ctio! !amed =#$D_S3L_:R3DE %hich accepts salary o" a! employee "i!ds
the correspo!di!+ salary +rade "rom S3L:R3DE ta)le a!d retur!s the +rade 'he
"u!ctio! should raise a! e,ceptio! i" the salary &alue does !ot "it i! a!y o" the salary
ra!+es speci"ied i! the sal+rade ta)le
Create a procedure C3LL_=#$D_S3L_:R3DE that does !ot accept a!y parameter 'he
procedure +i&es call to =#$D_S3L_:R3DE "or each record i! the emp ta)le )y passi!+ o!
the salary &alue "rom the curre!t record 'he procedure displays the correspo!di!+
employee !um)er- employee !ame a!d the salary +rade retur!ed )y =#$D_S3L_:R3DE-
o! the scree! 'he procedure should ha!dle error thro%! )y the "u!ctio! )y displayi!+
a! appropriate messa+e
:i&e a call to C3LL_=#$D_S3L_:R3DE throu+h a! a!o!ymous PL/SQL )loc.
PACKAGES
Practice A
Create a pac.a+e !amed M3$3:E_EMP_P3CF that has t%o pu)lic procedures- t%o
pac.a+e le&el &aria)les a!d a pri&ate "u!ctio! 'he pu)lic procedure >#RE_EMP adds a!
employee record i! EMP ta)le a!d the pu)lic procedure =#RE_EMP deletes a! employee
record "rom the EMP ta)le 'he t%o &aria)les &_i!sert_c!t a!d &_delete_c!t are used i!
the pac.a+e- "or .eepi!+ record o" the !um)ers o" times i!sert / delete has )ee!
e,ecuted
Create a pri&ate "u!ctio! ;3L#D3'E_EMP i! the pac.a+e to &alidate employee !um)er
'his "u!ctio! ca! )e called "rom >#RE_EMP a!d =#RE_EMP
'he "u!ctio! ;3L#D3'E_EMP accepts a! employee !um)er i! a parameter p_e!o a!d
retur!s 'RUE i" the &alue o" employee !um)er e,ists i! the EMP ta)le else it retur!s
=3LSE
'he procedure >#RE_EMP ta.es all the colum! &alues o" the EMP ta)le as parameters #t
+i&es a call to ;3L#D3'E_EMP )y passi!+ o! the &alue o" employee !um)er a!d i" the
"u!ctio! retur!s 'RUE the! it displays messa+e EEmployee !um)er already i! useG #" the
"u!ctio! retur!s =3LSE the! it i!serts a !e% record i! the EMP ta)le a!d displays a
messa+e E*!e employee addedG #t also i!creme!ts the &alue o" &_i!sert_c!t )y 1
'he procedure =#RE_EMP accepts a! employee !um)er as a parameter a!d +i&es a call
to ;3L#D3'E_EMP )y passi!+ o! the &alue o" employee !um)er #" the "u!ctio! retur!s
'RUE the! it deletes the correspo!di!+ record "rom the EMP ta)le- displays messa+e
E*!e employee deletedG a!d i!creme!ts the &alue o" &_delete_c!t )y 1 #" the "u!ctio!
retur!s =3LSE the! it displays messa+e E?ro!+ employee !um)erG
Chec. %or.i!+ o" the methods i! M3$3:E_EMP_P3CF )y +i&i!+ calls to the pu)lic
procedures a!d )y displayi!+ &alue o" the appropriate pac.a+e &aria)le- throu+h a!
a!o!ymous )loc.
2/B
PL/SQL Day 3
TRIGGERS
Practice 1
#mpleme!t the "ollo%i!+ )usi!ess rule %ith the help o" a tri++er !amed 'R_C>ECF_DEP'
a!d a procedure !amed SECURE_DML Cha!+es to data i! the dept ta)le- %ill )e allo%ed
o!ly i! the mo!th o" March
Create a procedure called SECURE_DML that pre&e!ts the DML stateme!t "rom
e,ecuti!+ i! a!y other mo!th tha! March #! case- a user tries to modi"y the ta)le i!
a!y other mo!th- the procedure should display a messa+e IYou ca! modi"y or add a
departme!t o!ly at the e!d o" a "i!a!cial yearJ
Create a stateme!t le&el tri++er'R_C>ECF_DEP' o! the dept ta)le that calls the a)o&e
procedure
'est it )y i!serti!+ a !e% record i! the dept ta)le
Practice 2
E!"orce re"ere!tial i!te+rity %ith a tri++er !amed 'R_C3SC3DE_C>3$:E ?he! the
&alue o" DEP'$* cha!+es i! the Dept ta)le- cascade the update to the correspo!di!+
ro%s i! the EMP ta)le
'est it )y updati!+ the &alue o" a dept!o "rom the dept ta)le


Practice 3
Create a tri++er !amed 'R_C>ECF_C*MM to impleme!t the "ollo%i!+ )usi!ess rule #!
EMP ta)le- employee ha&i!+ (o) as ESalesma!G should recei&e a commissio! 3 Salesma!
must recei&e a commissio! o" !ot less tha! Rs 1@@ Employees %ho are !ot sales
perso!s are !ot e!titled to +et commissio! 5comm &alue should )e $ULL7
'est it )y i!serti!+ a record i! the emp ta)le
Practice B
?hile modi"yi!+ the EMP ta)le- e!sure that the salary is i! the &alid ra!+e as speci"ied i!
the S3L:R3DE ta)le 5)et%ee! lo%est losal a!d hi+hest hisal7 %ith the help o" a tri++er
!amed 'R_;3L#D3'E_S3L
'est it )y updati!+ the salary &alue o" a! e,isti!+ record i! the emp ta)le
PracticeH
Create a ta)le !amed salaryLo+ %ith the appropriate colum!s a!d i!sert the emp!o-
!e% +rade- old salary a!d !e% salary &alues i! salaryLo+ ta)le i" the +rade o" a!
employee cha!+es
Create a tri++er !amed 'R_C>ECF_:R3DE that %ill )e "ired %he! a user modi"ies the
EMP ta)le #t %ill chec. %hether the +rade has cha!+ed )y ma.i!+ use o" the S3L:R3DE
ta)le 5:rade is depe!de!t o! Salary7 #" +rade is cha!+ed- the tri++er %ill lo+ the
correspo!di!+ employee !um)er- old salary- !e% salary a!d !e% +rade i!to salaryLo+
ta)le
3/B
PL/SQL Day 3
'est the %or.i!+ o" the tri++er )y "iri!+ a! appropriate DML Kuery
----
B/B

Das könnte Ihnen auch gefallen