Вы находитесь на странице: 1из 4

SA0951a Oracle Practical: PL/SQL Introduction

Remember PL/SQL does extra things that SQL cant do alone and therefore expands
SQL. Its especially useful for calculations, updates, audits, actions, erification etc.
In this lab you !ill try out some examples of anonymous bloc"s, !ith embedded SQL,
conditional branching #I$% and loops.
Task 1: Block structure
Remind yourself of the PL/SQL bloc" structure #!hich of the & "ey !ords are absolutely
necessary'%(
)*****
+*****
,*****
,*****-
Task 2: A first ea!"le
.ype in this bloc" and run it(
),/L0R,
interest /12S.02. 234+,R #&,5% (6 7.78-
amount 234+,R #&,5%-
+,9I2
amount (6 7:: ; interest-
,2)-
<ou should get the error message(
Declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: numer !reci"ion too lar#e
ORA-06512: at line 5
/an you explain !hat the problem is' ==================================
=================================================================
Task #:
+efore doing this create a ne! table called .,4P !ith three fields in it as follo!ing(
$IRS. >0R/?0R5#&%
S,/12) >0R/?0R5#5:%
.?IR) >0R/?0R5#7::%
<ou dont need to specify a primary "ey for this table@
2o! !rite a bloc" of PL/SQL !hich !ill I2S,R. the name and Aob of personnel number
B&CC into the .,4P table #into the S,/12) and .?IR) attributes aboe%.
Run it t!ice Aust to see that the insert goes in t!ice.
.ry the command R1LL+0/D no! and chec" the .,4P table again.
14/06/2014 Page 1
Task $: Tr% t&is one 'it& "artiall% co!"leted code to &el" (%ou fill in
t&e sections 'it& underscores)
I !ant to "no! !ho manages S.<L,S using a PL/SQL approach.
$eclare
manno numer%&'(
manname varc)ar2%10'(
man*o varc)ar2%10'(
man$e!t !er"onnel+$iv,t-!e(
$e!artment ranc),ro.t-!e( -- t)i" anc)or" a .)ole ro.
e#in
--t)e ne/t "tatement retrieve" t)e mana#er i$enti0ier 0or S12LES
"elect 3333333333 into 3333333333 0rom !er"onnel .)ere "urname45S12LES5(
-- ne/t "tatement retrieve" t)e $etail" o0 t)at mana#er+
"elect 3333333333333333333 into manname6 man*o6 man$e!t 0rom !er"onnel

7)ere 333333333333333333333(
--t)e ne/t line retrieve" an entire ro. containin# t)e ranc) in0o+
"elect * into $e!artment 0rom ranc) .)ere 33333333333333333333(
-- t)e ne/t line in"ert" t)e $iv into t)e 89RS1 attriute6
-- t)e name in to t)e SE:O;D attriute an$ ")oul$
-- in"ert a "trin# into t)e 1)ir$ attriute+
-- 2ou nee$ to com!lete t)e "trin# .it) t)e location+
in"ert into tem! value" %$e!artment+$iv6 manname6 man*o<< 5 in 5 <<
$e!artment+$ivname<<5 Divi"ion a"e$ in 5 << 3333333333333'(
en$(
Task 5: S"ot t&e *rrors in t&is code and correct t&e! +% inter"retin,
Oracle-s error detector
.he code belo! is designed to calculate the salary difference bet!een t!o dates. .ype it
in exactly as it is #better still, copy and paste it% so that you gain experience !ith 1racles
error detector. See if you can get the code !or"ing by correcting the numerous errors it
contains@
Declare;
v_diff varchar2(3)
v_startdate date="11-JUN-76";
v_enddate data:=!-"#r-$!;
%e&in
select 'a((salar))-'in(salar)) int* diff;
fr*' #ers*nnel +here ,*in_date %et+een startdate and
v_enddate;
d%'s_*-t#-t_#-t_line (.alar) difference %et+een /v_startdate //
and //enddate// is //v_diff);
end
14/06/2014 Page 2
Task .: /ore S*L*0T 1 I2TO
4a"e sure you hae run S,. S,R>,R13.P3. 12 #you only hae to do this once per
session unless you hae S,. S,R>,R13.P3. 1$$ at any time during the session%
/reate a PL/SQL program that !ill prompt for a surname to be entered. Ehen the
surname is entered, the follo!ing columns !ill be selected(
)I>204,
F1+.I.L,
S0L0R<
)isplay this as a sensible message
#e.g. STYLES is a CLERK who works in division 10 and earns 865%
Task 3: Anot&er one4
5rite a PL/SQL +lock to return t&e follo'in, infor!ation1116
G9ie me the aerage salary for all staff in any city reHuested by the userI
.hin" logically about ariables, datatypes, SQL commands and output.
3se J to prompt the user at runtime.
Task 7: Ifs 1166
Erite a procedure that accepts a score #bet!een : and 7::% and then grades that score
according to the rules belo!. 9ie a suitable output message.
C: K7::( 9rade 0
L: K MN( 9rade +
&: K 8N( 9rade /
5: K BN( 9rade )
O5:( 9rade ,
Task 9: Loo"s
a% ,xecute the $1R..L11P code belo! first and examine the output you get. 4a"e sure
you understand ho! the code !or"s #in particular you need to interpret the 41) function
P e.g. mod#B,B% is : because you get Qero !hen you diide B by B, but mod#8,B% is not
because you do not get an integer !hen you diide 8 by B%
Be,in
8or counter in 1661$ Loo"
If !od(counter9#):0 t&en
;+!s<out"ut6"ut<line(=Loo" counter di>isi+le +% # =??counter)@
*nd if@
*nd loo"@
*nd@
14/06/2014 Page 3
0lter the aboe code so that ,>,R< loop counter is printed out !ith an appropriate
message.
b%
/R,0., a ne! table #call it !hateer you li"e% !ith Aust one attribute of data type
234+,R#B%.
<ou dont need to specify a PD either as this is an unlin"ed table.
3se a E?IL, L11P to insert L ne! numbers into this table. .ry putting in the numbers
7::, 5::, B::, &::, 8:: and L::.
Run the procedure and correct any errors until it runs successfully.
/hec" the contents of the table.
Run it again #you simply need to type G/I at the prompt at this runs the last thing in
memory%
Run it again@ /hec" your table contents
.ype the R1LL+0/D command. /hec" table contents. See !hat has happened'
14/06/2014 Page 4

Вам также может понравиться