You are on page 1of 14

Section 2 Lesson 1: Using Variables in PL/SQL

Vocabulary
Identify the vocabulary word for each definition below:
Use variables- Used for storage of data and manipulation of stored values.
Parameters- values passed to a program by a user or by another program to customize the
program.
Try It / Solve It

1. Fill in the blanks.
A. ariables can be assigned to the output of a PL/SQL subprogram
!. ariables can be assigned values in the declarative section of a "#$%&# block
'. ariables can be passed as parameters to PL/SQL to subprograms.
(. Identify valid and invalid variable declaration and initialization
number)of)copies "#%)I*+,-,./
printer)name '0*%+A*+ A.'1A.(2134/
deliver)to A.'1A.(2134:56ohnson/
by)when 7A+,:5 %8%7A+,91/
:. ,;amine the following anonymous block and choose the appropriate statement.

7,'#A.,
fname A.'1A.(2(34/
lname A.'1A.(21<4 7,FAU#+ =fernandez=/
!,-I*
7!>%)0U+"U+."U+)#I*,2 F*A>, ??= = ??lname4/
,*7/
A. +he block will e;ecute successfully and print @fernandezA.
!. +he block will give an error because the fname variable is used without initializing.
'. +he block will e;ecute successfully and print @null fernandezA .
7. +he block will give an error because you cannot use the 7,FAU#+ keyword to initialize a variable
of the A.'1A.( type.
,. +he block will give an error because the F*A>, variable is not declared.
B. In Application ,;press:

A. 'reate the following function:

'.,A+, FU*'+I0* num)characters 2p)string I* A.'1A.(4
.,+U.* I*+,-,. A%
v)num)characters I*+,-,./
!,-I*
%,#,'+ #,*-+12p)string4 I*+0 v)num)characters
F.0> dual/
.,+U.* v)num)characters/
,*7/

!. 'reate and e;ecute the following anonymous block:

7,'#A.,
v)length)of)string I*+,-,./
!,-I*
v)length)of)string :5 num)characters2=0racle 'orporation=4/
7!>%)0U+"U+."U+)#I*,2v)length)of)string4/
,*7/

<. Crite an anonymous block that uses a country name as input and prints the highest and
lowest elevations for that country. Use the wf)countries table. ,;ecute your block three
times using United %tates of AmericaD French .epublicD and 6apan.
Section 2 Lesson 2: Recogniing PL/SQL Le!ical Units

Vocabulary
Identify the vocabulary word for each definition below:
Literal E An e;plicit numericD character stringD dateD or !oolean value that is not represented by
an identifier
"elimiters E %ymbols that have special meaning to an 0racle database.
Reserve# $or#s E Cords that have special meaning to an 0racle database and cannot be used
as identifiers.
%omments E 7escribe the purpose and use of each code segment and are ignored by "#$%&#.
Le!ical unitsE !uilding blocks of any "#$%&# block and are seFuences of characters including
lettersD digitsD tabsD returnsD and symbols.
I#enti&iers E A nameD up to :3 characters in lengthD given to a "#$%&# obGect.
Try It / Solve It Questions
1. Fill in the blanks.
A. An identifier is the name given to a "#$%&# obGect.
!. A reserved word is a word that has special meaning to the 0racle database.
'. A delimiters is a symbol that has special meaning to the 0racle database.
7. A literal is an e;plicit numericD character stringD dateD or !oolean value that is not
represented by an identifier.
,. A comments e;plains what a piece of code is trying to achieve.
(. Identify each of the following identifiers as valid or invalid. If invalidD specify why.

Identifier alid 2H4 Invalid2H4 Chy InvalidI
+oday ;
#ast name ; 'ontine un spatiu
+odayAs)date H
number)of)days)in)february)this) year ; >ai mult de :3 de caractere
IsleapJyear H
Knumber H
*U>!,.K H
*umber1toL H
:. Identify the reserved words in the following list.

Cord .eservedI
8$*
create 7a
make *u
table 7a
seat *u
alter 7a
rename 7a
row 7a
number 7a
web *u
B. Chat kind of le;ical unit 2for e;ample .eserved wordD 7elimiterD #iteralD 'omment4 is each of the
followingI

alue #e;ical Unit
%,#,'+ .eserved word
:5 'ompound 7elimiter
=+,%+= 'omment
FA#%, !oolean #iteral
EE new process 'omment
F.0> .eserved word
$Mselect the country with the highest elevation M$ 'omment
)test
B.3N *umeric comment









Section 2 Lesson ': Recogniing "ata Ty(es
Vocabulary
Identify the vocabulary word for each definition below:
Large ob)ectE 1old valuesD called locatorsD that specify the location of large obGects 2such as
graphic images4 that are stored out of line.
Scalar E 1old a single value with no internal components.
%om(osite E 'ontain internal elements that are either scalar 2record4 or composite 2record and
table4
Re&erence E 1old valuesD called pointersD that point to a storage location.
*b)ect E A schema obGect with a nameD attributesD and methods.
Try It / Solve It
1. In your own wordsD describe what a data type is and e;plain why it is important.
Rezolvare : specificO ce tip i ce dimensiune de date poate fi utilizatD precum i modul Pn care
acesta este stocat




Section 2 Lesson 4: Using Scalar Data Types
Vocabulary
Identify the vocabulary word for each definition below:
+oolean E A datatype that stores one of the three possible values used for logical
calculations: TRUE, FALE, or !ULL"
,T-P. - Attribute used to declare a variable according to another previously
declared variable or database colu#n"
Try It / Solve It
1. $eclarations:
A" %hich of the following variable declarations are valid&
Declaration Valid or
Invalid
a
nu#ber'of'student
s (L'I!TE)ER* Valid
b TU$E!T'!A+E ,AR-.AR/012345ohnson*
Invalid
c stu'per'class -6!TA!T !U+7ER*
Invalid
d to#orrow $ATE :4 8$ATE91* Valid
!. For those declarations in 1"A" that are invalid, describe why they are invalid"
Rezolvare :c" constanta nu are nicio valoare atribuita
'. %rite an anony#ous bloc: in which you declare and print each of the variables in 1"A,
correcting the invalid declarations"
Rezolvare:
7,'#A.,
number)of)students "#%)I*+,-,./
%+U7,*+)*A>, A.'1A.(2134:56ohnson/
stu)per)class '0*%+A*+ *U>!,.2:D(4 :5Q.(</
tomorrow 7A+, :5 %8%7A+,91/
!,-I*
7!>%)0U+"U+."U+)#I*,2= number)of)students 5 =?? number)of)students4/
7!>%)0U+"U+."U+)#I*,2= %+U7,*+)*A>, 5 =?? %+U7,*+)*A>,4/
7!>%)0U+"U+."U+)#I*,2= stu)per)class 5 =?? stu)per)class4/
7!>%)0U+"U+."U+)#I*,2= tomorrow 5 =?? tomorrow4/
,*7/
(. Evaluate the variables in the following code" Answer the following ;uestions about each
variable" Is it na#ed well& %hy or why not& If it is not na#ed well, what would be a better
na#e and why&
$E-LARE
country'na#e ,AR-.AR/ 0<23*
#edian'age !U+7ER0=,/3*
7E)I!
ELE-T country'na#e, #edian'age I!T6 country'na#e, #edian'age
FR6+ wf'countries
%.ERE country'na#e 4 >United tates of A#erica>*
$7+'6UT(UT"(UT'LI!E0> The #edian age in >??country'na#e??> is
>??#edian'age??>">3*
E!$*
Rezolvare: variabilele country'na#e si #edian'age sunt folosite ca identificatori, desi
desfinesc si nu#ele a doua ca#puri din tabela wf'countries
:. E@a#ine the declarations in ;uestion /" -hange the declarations so that they use the
AT8(E attribute"
Rezolvare:
$E-LARE
v)country)name wf)countries.country)nameR+8",/
v)median)age wf)countries.median)ageR+8",:
B. In your own words, describe why using the AT8(E attribute is better than hardB
coding data types" -an you e@plain how you could run into proble#s in the future by
hardBcoding the data types of the country'na#e and #edian'age variables in
;uestion /&
Rezolvare: Este folosit pentru a oferi Cn #od auto#at o variabilD cu acelea i date
tipu i di#ensiuneca defini iea coloanei de baEe de date,o altD variabilD declaratD
<. -reate the following anony#ous bloc::
7E)I!
$7+'6UT(UT"(UT'LI!E0>.ello %orld>3*
E!$*
A " Add a declarative section to this (LFGL bloc:" In the declarative section, declare
the following variables:
S A variable na#ed T6$A8 of datatype $ATE" InitialiEe T6$A8 with 8$ATE"
S A variable na#ed T6+6RR6% with the sa#e datatype as T6$A8" Use the
AT8(E attribute to declare this variable"
Rezolvare:
$E-LARE
T6$A8 $ATE:48$ATE*
T6+6RR6% T6$A8AT8(E*
! " In the e@ecutable section, initialiEe the T6+6RR6% variable with an e@pression
that calculates to#orrowHs date 0add 1 to the value in T6$A83" (rint the value of T6$A8
and T6+6RR6% after printing I.ello %orldH"
Rezolvare:
7E)I!
T6+6RR6% :4 T6$A8 91*
$7+'6UT(UT"(UT'LI!E0>.ello %orld>??> >??T6$A8??> >??T6+6RR6%3*
E!$*
Hello World 03/Mar/2014 04/Mar/2014
Statement processed.
Section 2 Lesson 5: Writing L/S!L "#ecutable State$ents
Vocabulary
Identify the vocabulary word for each definition below:
.!(licit %onversions- 'onverts values from one data type to another by using builtEin
functions.
Im(licit conversionsE 'onverts data types dynamically if they are mi;ed in a
statement.
Try it/solve it
1" E@a#ine the following code and then answer the ;uestions"
$E-LARE
@ ,AR-.AR/0/23*
7E)I!
@:4 >1/J> 9 >K<=> *
$7+'6UT(UT"(UT'LI!E0@3*
E!$*
3 A" %hat do you thin: the output will be when you run the above code&
Rezolvare: <LM
1 7" !ow, run the code" %hat is the output&
Rezolvare: <LM
( -" In your own words, describe what happened when you ran the code" $id any
i#plicit conversions ta:e place&
Rezolvare: A avut loc o conversie i#plicita
(. %rite an anony#ous (LFGL bloc: that assigns the progra##erHs full na#e to a variable,
and then displays the nu#ber of characters in the na#e"
Rezolvare:
$E-LARE
nu#e ,AR-.AR/0J23*
lung I!TE)ER0J3*
7E)I!
nu#e:4 >-ornea +adalina>*
lung :4 LE!)T.0nu#e3*
$7+'6UT(UT"(UT'LI!E0lung3*
E!$*
:. %rite an anony#ous (LFGL bloc: that uses today>s date and outputs it in the for#at of
I+onth dd, yyyyH" tore the date in a $ATE variable called #y'date" -reate another
variable of the $ATE type called v'last'day" Assign the last day of this #onth to
v'last'day" $isplay the value of v'last'day"
Rezolvare:
$E-LARE
#y'date $ATE:48$ATE*
v'last'day $ATE :4 LAT'$A80#y'date3*
7E)I!
$7+'6UT(UT"(UT'LI!E0T6'-.AR0#y'date,>+onth dd,
8888>33*
$7+'6UT(UT"(UT'LI!E0,'LAT'$A83*
E!$*
March 03, 2014
31/Mar/2014
Statement processed.
B. +odify the progra# created in ;uestion J to add K< days to todayHs date and then
calculate and display the nu#ber of #onths between the two dates"
Rezolvare:
$E-LARE
#y'date $ATE:48$ATE*
v'last'day $ATE :4 #y'date9K<*
7E)I!
$7+'6UT(UT"(UT'LI!E 0+6!T.'7ET%EE!0v'last'day,
#y'date33*
$7+'6UT(UT"(UT'LI!E0,'LAT'$A83*
E!$*
1.4516129032258064516129032258064516129
17/pr/2014
<" E@a#ine the following code and then answer the ;uestions"
$E-LARE
@ !U+7ER0=3*
7E)I!
@ :4 < 9 J N / *
$7+'6UT(UT"(UT'LI!E0@3*
E!$*
A. %hat do you thin: the output will be when you run the above code&
Rezolvare: 11
!. !ow run the code" %hat is the output&
Rezolvare: 11
Q. E@a#ine the following code and then answer the ;uestion"
$E-LARE
v'nu#ber !U+7ER*
v'boolean 766LEA!*
7E)I!
v'nu#ber :4 /<*
v'boolean :4 !6T0v'nu#ber O J23*
E!$*
%hat value is assigned to v'boolean&
L. List two drawbac:s to relying on i#plicit data type conversions"
Rezolvare: (ot fi incete
codul folosit este #ai greu de inteles si parcurs
Section 2 Lesson /: 0este# +loc1s an# Variable Sco(e
Vocabulary
Identify the vocabulary word for each definition below.
QU2LI3I.R- A label given to a block.
Variable Sco(e E'onsists of all the blocks in which the variable is either local 2the declaring
block4 or global 2nested blocks within the declaring block4 .
Variable VisibilityE+he portion of the program where the variable can be accessed without
using a Fualifier.
Try It / Solve It
1. ,valuate the "#$%&# block below and determine the value of each of the following
variables according to the rules of scoping.
7,'#A.,
weight *U>!,.2:4 :5 Q33/
message A.'1A.(2(<<4 :5 ="roduct 1331(=/
!,-I*
7,'#A.,
weight *U>!,.2:4 :5 1/
message A.'1A.(2(<<4 :5 ="roduct 11331=/
new)locn A.'1A.(2<34 :5 =,urope=/
!,-I*
weight :5 weight 9 1/
new)locn :5 =Cestern = ?? new)locn/
EE "osition 1 EE
,*7/
weight :5 weight 9 1/
message :5 message ?? = is in stock=/
EE "osition ( EE
,*7/
A.+he value of weight at position 1 is: (
!.+he value of new)locn at position 1 is: ,urope
'.+he value of weight at position ( is: Q31
7.+he value of message at position ( is: "roduct 1133( is in stock
,.+he value of new)locn at position ( is: nu e;ista
(. ,nter and run the following "#$%&# blockD which contains a nested block. #ook at the output and
answer the Fuestions.
7,'#A.,
v)employee)id employees.employee)idR+8",/
v)Gob employees.Gob)idR+8",/
!,-I*
%,#,'+ employee)idD Gob)id I*+0 v)employee)idD v)Gob
F.0> employees
C1,., employee)id 5 133/
7,'#A.,
v)employee)id employees.employee)idR+8",/
v)Gob employees.Gob)idR+8",/
!,-I*
%,#,'+ employee)idD Gob)id I*+0 v)employee)idD v)Gob
F.0> employees
C1,., employee)id 5 13:/
7!>%)0U+"U+."U+)#I*,2v)employee)id?? = is a =??v)Gob4/
,*7/
7!>%)0U+"U+."U+)#I*,2v)employee)id?? = is a =??v)Gob4/
,*7/
A. Chy does the inner block display the Gob)id of employee 13:D not employee 133I
3 Rezolvare: pentru ca se afiseaza in blocul unde se e;trage valare 13:
!. Chy does the outer block display the Gob)id of employee 133D not employee 13:I
Rezolvare: pentru ca valoarea variabilei este cea din blocul e;tern
'. >odify the code to display the details of employee 133 in the inner block. Use block labels.
TTouterUU
Section 2 Lesson %: &ood rogra$$ing ractices
Try It / Solve It
1"Enter and run the following (LFGL bloc:" It will e@ecute correctly if you have entered it
correctly, but it contains so#e e@a#ples of bad progra##ing practice"
$E-LARE
#yvar1 ,AR-.AR/0/23* #yvar/
nu#ber0K3*
7E)I!
ELE-T country'na#e I!T6 #yvar1
FR6+ wf'countries %.ERE country'id 4 1/K=*
#yvar/ :4
>1/JK>*
+8,AR/ :4 #yvar/ N /*
$7+'6UT(UT"(UT'LI!E0#yvar13* End*
:
B
< A" +odify the bloc: to use good progra##ing practice, and reBrun the bloc:"
Q 7" 8our #odified bloc: should contain e@a#ples of the following good progra##ing
practices: e@plicit data type conversions, #eaningful and consistent variable na#es, use of
AT8(E, upper and lowercase conventions, single and #ultiBline co##ents, and clear
indentation"
$E-LARE
#yvar1 ,AR-.AR/0/23*
#yvar/ !U+7ER0K3*
7E)I!
ELE-T country'na#e I!T6 #yvar1
FR6+ wf'countries %.ERE country'id 4 1/K=*
#yvar/ :4 >1/JK>*
+8,AR/ :4 #yvar/ N /*
$7+'6UT(UT"(UT'LI!E0#yvar13*
E!$*

You might also like