Professional Documents
Culture Documents
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
Oracle Database 12c: PL/SQL e to
FundamentalsIG O ens
T
S ble l i c
P O
Z sfera
Activity Guide
E
I G U ran
D R on-t
O an
A R has uideฺ
R
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
D80182GC11
Edition 1.1
July 2014
D87353
Edutronic Global Services EฺIฺRฺL
Disclaimer
This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and
print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way.
Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization
of Oracle.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
The information contained in this document is subject to change without notice. If you find any problems in the document, please
report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.
If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United
States Government, the following notice is applicable:
Trademark Notice
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
u se
to
owners.
G O e n se
Author
S TI e lic
Dimpi Rani Sarmah
Z PO erabl
Technical Contributors and Reviewers U E nsf
Nancy Greenberg, Swarnapriya Shridhar, KimSeong R G Miyuki
ILoh,
n - t raOsato, Laszlo Czinkoczki,
D
Wensley no
Madhavi Siddireddy, Jim Spiller, Christopher O
A R has a uideฺ
R
This book was published using:
C ESOracleo m ) Tutor
n t G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
ioro
(jul
Edutronic Global Services EฺIฺRฺL
Table of Contents
Practices for Lesson 1: Introduction ........................................................................................................ 1-1
Practices for Lesson 1............................................................................................................................. 1-2
Practice 1-1: Getting Started ................................................................................................................... 1-3
Solution 1-1: Getting Started ................................................................................................................... 1-5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
Z PO erabl
Practice 6: Writing Control Structures ......................................................................................................
Solution 6: Writing Control Structures ...................................................................................................... 6-4
U E s f
R IG -tran
Practices for Lesson 7: Working with Composite Data Types ................................................................. 7-1
O D non
Practice 7: Working with Composite Data Types ...................................................................................... 7-2
R a e ฺ
Solution 7: Working with Composite Data Types ...................................................................................... 7-4
Practices for Lesson 8: Using Explicit Cursors
S A ) h as Guid
R....................................................................................... 8-1
Practice 8-1: Using Explicit CursorsE
Solution 8-1: Using Explicit O c o m ent
C ......................................................................................................... 8-2
@ th .......................................................................................... 8-11
Solution 8-2: Using Explicit Cursors: Optional
2
Practices for Lesson
e z 389: Predefined
Handling Exceptions .......................................................................................... 9-1
r i g u
Practice 9-1: Handling Exceptions .......................................................................................... 9-2
d
Solution 9-1: Handling Predefined Exceptions .......................................................................................... 9-3
O DR non-
Practice 9-1: Handling Exceptions ...........................................................................................................
Solution 9-1: Handling Exceptions ...........................................................................................................
19-2
19-3
R
R has uide a ฺ
A
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
ioro
(jul
u se
e to
Practices for Lesson
I G 1: ens
O
T
Introduction OS ble l
ic
Z P era
Chapter 1 UE
n s f
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Lesson Overview
In these practices, you do the following:
• Start SQL Developer
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
TI e lic
a. Expand the MyConnection connection by clicking the plus symbol next to it.
S
Z PO erabl
b. Expand the Tables icon by clicking the plus symbol next to it.
c. Display the structure of the EMPLOYEES table.
U E nsf
5. IG -tra
Use the Data tab to view data in the EMPLOYEES table.
R
6. D non
Use the SQL Worksheet to select the last names and salaries of all employees whose
O
A R R has a uideฺ
annual salary is greater than $10,000. Use both the Execute Statement (F9) and the Run
Script (F5) icons to execute the SELECT statement. Review the results of both methods of
ES om) nt G
executing the SELECT statements on the appropriate tabs.
C
IO ailฺc tude
Note: Take a few minutes to familiarize yourself with the data, or consult Appendix A, which
L
JU gm his S
provides the description and data for all the tables in the HR schema that you will use in this
course.
2 @ t
8
7.
u
appears.
g ez3
From the SQL Developer menu, select Tools > Preferences. The Preferences window
8.od r i
Select Database > Worksheet Parameters. In the “Select default path to look for scripts”
i o r
(j ul text box, use the Browse button to select the /home/oracle/labs/plsf directory. This
directory contains the code example scripts, lab scripts, and practice solution scripts that
are used in this course. Then, in the Preferences window, click OK to save the Worksheet
Parameter setting.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
2. Create a database connection by using the following information (Hint: Select the Save
Password check box):
a. Connection Name: MyConnection
b. Username: ora41
c. Password: ora41
d. Hostname: localhost
e. Port: 1521
u se
f. SID: orcl to
O nse
Right-click the Connections node on the Connections tabbed page and select New
G e
Connection…
S TI e lic
PO erabl
Result: The New/Select Database Connection window appears.
Z
E nsf
Use the preceding information to create the new database connection. In addition, select the
U
Save Password check box. Example:
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
3. Test the new connection. If the Status is Success, connect to the database by using this
new connection.
a. In the Database Connection window, click the Test button.
Note: The connection status appears in the lower-left corner of the window.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
b. If the status is Success, click the Connect button. E
U n s f
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
ioro
(jul
Note: To display the properties of an existing connection, right-click the connection name
on the Connections tab and select Properties from the shortcut menu.
4. Browse the structure of the EMPLOYEES table and display its data.
a. Expand the MyConnection connection by clicking the plus symbol next to it.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
IG table.-tra
5. Use the Data tab to view the data in the EMPLOYEES
R
O
Result: The EMPLOYEES table data is displayed n n
D as follows:
o
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
l i o ro
(j u
6. Use the SQL Worksheet to select the last names and salaries of all employees whose
annual salary is greater than $10,000. Use both the Execute Statement (F9) and Run Script
(F5) icons to execute the SELECT statement. Review the results of both methods of
executing the SELECT statements on the appropriate tabs.
Note: Take a few minutes to familiarize yourself with the data, or consult Appendix A, which
provides the description and data for all the tables in the HR schema that you will use in this
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
course.
To display the SQL Worksheet, click the MyConnection tab.
Note: This tab was opened previously when you drilled down on your database connection.
Enter the appropriate SELECT statement. Press F9 to execute the query and F5 to execute the
query by using the Run Script method.
For example, when you press F9, the results appear similar to the following:
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
7. From the SQL Developer menu, select Tools > Preferences. The Preferences window
appears.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
8. Select Database > Worksheet Parameters. In the “Select default path to look for scripts”
text box, use the Browse button to select the /home/oracle/labs/plsf directory. u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
This directory contains the code example scripts, lab scripts, and practice solution scripts that
are used in this course.
Click Select to choose the directory.
Then, in the Preferences window, click OK to save the Worksheet Parameter setting.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
9. Familiarize yourself with the structure of the /home/oracle/labs/plsf directory.
R I G -tra
O D non
a. Select File > Open. Navigate to the /home/oracle/labs/plsf directory. This
R R has a uideฺ
directory contains three subdirectories:
A
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
l i o ro • The /code_ex directory contains the code examples found in the course
(j u materials. Each .sql script is associated with a particular page in the lesson.
• The /labs directory contains the code that is used in certain lesson practices.
You are instructed to run the required script in the appropriate practice.
• The /soln directory contains the solutions for each practice. Each .sql script is
numbered with the associated practice_exercise reference.
b. You can also use the Files tab to navigate through directories to open script files.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e n se
S TI e lic
Z PO erabl
U E nsf
c. Using the Open window, and the Files tab, navigate
script file without executing the code. DR n - tra the directories and open a
IG through
d. Close the SQL Worksheet. R O a no ฺ
To close any SQL Worksheet tab,S ARX on) the
click h s as shown
atab, u idehere:
C E om n t G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
ioro
(jul
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Practices for Lesson
I G 2: ens
O
Introduction to S T e lic
PL/SQL
P era O bl
Chapter 2 UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Lesson Overview
The /home/oracle/labs/plsf/labs folder is the working directory where you save the
scripts that you create.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
The solutions for all the practices are in the /home/oracle/labs/plsf/soln folder.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
b. DECLARE
v_amount INTEGER(10);
END;
c. DECLARE
BEGIN
END;
d. DECLARE
v_amount INTEGER(10);
BEGIN
DBMS_OUTPUT.PUT_LINE(v_amount);
END;
2. Create and execute a simple anonymous block that outputs “Hello World.” Execute and
save this script as lab_02_02_soln.sql. u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
v_amount INTEGER(10);
END;
c. DECLARE
BEGIN
END;
d. DECLARE
v_amount INTEGER(10);
BEGIN
DBMS_OUTPUT.PUT_LINE(v_amount);
END;
The block in a does not execute. It has no executable statements.
The block in b does not have the mandatory executable section that starts with the BEGIN
keyword. u se
to
The block in c has all the necessary parts, but no executable statements.
G O e nse
The block in d executes successfully.
S TI e lic
save this script as lab_02_02_soln.sql. Z PO erabl
2. Create and execute a simple anonymous block that outputs “Hello World.” Execute and
U E nsf
Enter the following code in the workspace, and then press F5.
R IG -tra
SET SERVEROUTPUT ON
O D non
BEGIN
R RWorld s a ideฺ
DBMS_OUTPUT.PUT_LINE(' Hello
S A ) a
h t Gu');
END; E m
Coutputฺconothe Script n tab:
I O
You should see the following i l u d eOutput
L
JU gma his St
2 @ t
z3 8
u e
d r ig
ioro
(jul
Click the Save button. Select the folder in which you want to save the file. Enter
lab_02_02_soln.sql as the file name and click Save.
u se
e to
Practices for Lesson
I G 3: ens
O
Declaring PL/SQLS TVariables
e l ic
P era O bl
Chapter 3 UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
b. last_name
c. today’s_date
d. Number_of_days_in_February_this_year
e. Isleap$year
f. #number
g. NUMBER#
h. number1to7
2. Identify valid and invalid variable declaration and initialization:
a. number_of_copies PLS_INTEGER;
b. PRINTER_NAME constant VARCHAR2(10);
u se
c. deliver_to VARCHAR2(10):=Johnson;
to
d. by_when DATE:= CURRENT_DATE+1;
G O e n se
3. TI e lic
Examine the following anonymous block, and then select a statement from the following
S
that is true.
Z PO erabl
DECLARE
U E nsf
v_fname VARCHAR2(20);
R IG -tra
v_lname VARCHAR2(15) DEFAULT 'fernandez';
O D non
BEGIN
A R R h||'a s'a||v_lname);
i d eฺ
DBMS_OUTPUT.PUT_LINE(v_fname
E S ) G u
END;
C ฺco m en t
I
L m O a i l u d
t prints “fernandez.”
JUexecutes
a. The block
g successfully
i s Sand
@
b. The block produces
2 th because the fname variable is used without initializing.
an error
z 38executes successfully and prints “null fernandez.”
c. The block
e
d. ig
r u block produces an error because you cannot use the DEFAULT keyword to
The
d initialize a variable of type VARCHAR2.
ioro
(jul e. The block produces an error because the v_fname variable is not declared.
u se
to
G O e n se
S TI e lic
5. Edit the lab_03_04_soln.sql script.
Z PO erabl
U E nsf and
a. Add code to create two bind variables, named b_basic_percent
b_pf_percent. Both bind variables are ofR IG NUMBER.
type
n - t ra
ODblock,
b. In the executable section of the PL/SQL
R a
o theฺ values 45 and 12 to
nassign
R has respectively.
b_basic_percent and b_pf_percent,
A u i de
c. Terminate the PL/SQLE S with “/”
block t G the value of the bind variables by
) and ndisplay
C o m
L IOcommand.
using the PRINT
a i lฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
l i o ro
(j u
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
DECLARE
v_today DATE:=SYSDATE;
2) Variable v_tomorrow of type today. Use the %TYPE attribute to declare this
variable.
v_tomorrow v_today%TYPE;
b. In the executable section of the PL/SQL block, assign the values 45 and 12 to
b_basic_percent and b_pf_percent, respectively.
:b_basic_percent:=45;
:b_pf_percent:=12;
c. Terminate the PL/SQL block with “/” and display the value of the bind variables by
using the PRINT command.
/
PRINT b_basic_percent
PRINT b_pf_percent
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
OR 2 @ t
8
g u ez3
r i
PRINT
i orod
(jul
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Practices for Lesson I G 4: ens
O
S
Writing Executable
T e lic
P O abl
StatementsZ r
G U ran E sfe
I
R 4 on-t
Chapter
D
O an
A R has uideฺ
R
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Note: If you have executed the code examples for this lesson, make sure you execute the
following code before starting this practice:
DROP sequence my_seq;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
DECLARE
v_weight NUMBER(3) := 600;
v_message VARCHAR2(255) := 'Product 10012';
BEGIN
DECLARE
v_weight NUMBER(3) := 1;
v_message VARCHAR2(255) := 'Product 11001';
v_new_locn VARCHAR2(50) := 'Europe';
BEGIN
se
v_weight := v_weight + 1;
v_new_locn := 'Western ' || v_new_locn;
to u
1
END;
G O e n se
v_weight := v_weight + 1;
S TI e lic
v_message := v_message || ' is in stock';
Z PO erabl
2
v_new_locn := 'Western ' || v_new_locn;
U E nsf
END; R IG -tra
/ O D non
1. Evaluate the preceding PL/SQL block
A RR s
and determine
a eฺ type and value of each of the
a theiddata
following variables, according
E Sto the rules
) hof scoping.
G u
C atฺpositionm
o 1 dis:en t
O
a. The value of v_weight
I i l c
L ma at position tu 1 is:
b. The value JUof v_new_locn S
is 2 is:
g atthposition
@
c. The value of2v_weight
z
d. The value
e 38of v_message at position 2 is:
e.rig u value of v_new_locn at position 2 is:
The
d
ioro
(jul DECLARE
v_customer VARCHAR2(50) := 'Womansport';
v_credit_rating VARCHAR2(50) := 'EXCELLENT';
BEGIN
DECLARE
v_customer NUMBER(7) := 201;
v_name VARCHAR2(25) := 'Unisports';
BEGIN
v_credit_rating :='GOOD';
…
END;
…
END;
2. In the preceding PL/SQL block, determine the values and data types for each of the
following cases:
a. The value of v_customer in the nested block is:
b. The value of v_name in the nested block is:
c. The value of v_credit_rating in the nested block is:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u e z3 for the calculation. Try to use only one expression to calculate the PF. Print
variables
drig
the employee’s salary and his or her contribution toward PF.
ior o g. Execute and save your script as lab_04_03_soln.sql. The sample output is as
( j u l follows:
DECLARE
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
DECLARE
v_customer VARCHAR2(50) := 'Womansport';
v_credit_rating VARCHAR2(50) := 'EXCELLENT';
BEGIN
DECLARE
v_customer NUMBER(7) := 201;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
2. In the preceding PL/SQL block, determine the values and data types for each of the
following cases:
a. The value of v_customer in the nested block is:
201
The data type is NUMBER. u se
to
b. The value of v_name in the nested block is:
G O e n se
Unisports
The data type is VARCHAR2. S TI e lic
is: P
O abl
E nsfer
c. The value of v_credit_rating in the nested block Z
GOOD
The data type is VARCHAR2.
U
IG -tra
D R n
d. The value of v_customer in the main O block is:no
Womansport
A R R has a uideฺ
ES om) nt G
The data type is VARCHAR2.
C
e. The value of v_name
IO inathe ilinฺcmain block eis:
udblock
Null. nameUisLnot visible the S t
main and you would see an error.
J g m i s
@
f. The value of v_credit_rating
2 th in the main block is:
z
Theedata
38type is VARCHAR2.
EXCELLENT
r i g u
3. d
Use the same session that you used to execute the practices in the lesson titled “Declaring
o PL/SQL Variables.” If you have opened a new session, execute lab_03_05_soln.sql.
j u l ior
( Then, edit lab_03_05_soln.sql as follows:
a. Use single-line comment syntax to comment the lines that create the bind variables,
and turn on SERVEROUTPUT.
b. Use multiple-line comments in the executable section to comment the lines that assign
values to the bind variables.
/*:b_basic_percent:=45;
:b_pf_percent:=12;*/
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
DECLARE
v_basic_percent NUMBER:=45;
v_pf_percent NUMBER:=12;
se
v_fname VARCHAR2(15);
v_emp_sal NUMBER(10);
to u
G O e n se
d. Include the following SQL statement in the executable section:
S TI e lic
SELECT first_name, salary INTO v_fname, v_emp_sal Z PO erabl
FROM employees WHERE employee_id=110; U E nsf
R IG -tra
O
e. Change the line that prints “Hello World” o n and the first name. Then,
Dto printn“Hello”
comment the lines that displayRtheRdates s aprint the
and d ฺ variables.
ebind
S A a
h t Gu i
E
C Hello m ) n
O
DBMS_OUTPUT.PUT_LINE('
I l ฺ c o '||dev_fname);
/*
U L ma i
DBMS_OUTPUT.PUT_LINE('TODAY
S t u IS : '|| v_today);
J g this
DBMS_OUTPUT.PUT_LINE('TOMORROW IS : ' || v_tomorrow);*/
... 2 @
...
e z 38
r i g u
d
ioro--PRINT b_basic_percent
/
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Practices for Lesson I G O
5: Using
c e ns
SQL Statements
T e ali
Swithin
P O
r a bl
E Z
PL/SQL Block sfe
I G U ran
R 5 on-t
Chapter
D
O an
A R has uideฺ
R
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Note: If you have executed the code examples for this lesson, make sure you execute the
following code before starting this practice:
DROP table employees2;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
3. In step 2, you set location_id to NULL. Create a PL/SQL block that updates
location_id to 3000 for the new department.
Note: If you have successfully completed step 2, continue with step 3a. If not, first execute
the solution script /soln/sol_05_02.sql.
a. Start the executable block with the BEGIN keyword. Include the UPDATE statement to
set location_id to 3000 for the new department (v_dept_id =280).
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
b. End the executable block with the END keyword. Terminate the PL/SQL block with “/”
and include a SELECT statement to display the department that you updated.
c. Include a DELETE statement to delete the department that you added.
d. Execute and save your script as lab_05_03_soln.sql. The sample output is as
follows:
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
DECLARE
v_max_deptno NUMBER;
b. Start the executable section with the BEGIN keyword and include a SELECT statement
to retrieve the maximum department_id from the departments table.
BEGIN
SELECT MAX(department_id) INTO v_max_deptno FROM
departments;
u se
to
c. Display v_max_deptno and end the executable block.
G O e n se
DBMS_OUTPUT.PUT_LINE('The maximum department_idSis : 'le||
TI lic
v_max_deptno);
Z PO erab
END;
U E nsf
R IG -tra
O D non The sample output is as
d. Execute and save your script as lab_05_01_soln.sql.
follows:
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
2. Modify the PL/SQLJU block g h
S in step 1 to insert a new department into the
mthat youiscreated
departments table.
2 @ t
a. Load z the 8
3 lab_05_01_soln.sql script. Declare two variables:
g u e of type departments.department_name and
d r i v_dept_name
of type
r o v_dept_id NUMBER
b. You have already retrieved the current maximum department number from the
departments table. Add 10 to it and assign the result to v_dept_id.
v_dept_id := 10 + v_max_deptno;
…
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
d. Use the SQL attribute SQL%ROWCOUNT to display the number of rows that are affected.
e. Execute a SELECT statement to check whether the new department is inserted. You
can terminate the PL/SQL block with “/” and include the SELECT statement in your
script. u se
to
G O e n se
…
S TI e lic
PO 280; bl
/
SELECT * FROM departments WHERE department_id=
Z r a
U E nsfe
IG -traThe sample output is as
f. Execute and save your script as lab_05_02_soln.sql.
R
follows: O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
ro drig
l i o
(ju 3. In step 2, you set location_id to NULL. Create a PL/SQL block that updates the
location_id to 3000 for the new department.
Note: If you successfully completed step 2, continue with step 3a. If not, first execute the
solution script /soln/sol_05_02.sql.
a. Start the executable block with the BEGIN keyword. Include the UPDATE statement to
set location_id to 3000 for the new department (v_dept_id =280).
BEGIN
UPDATE departments SET location_id=3000 WHERE
department_id=280;
b. End the executable block with the END keyword. Terminate the PL/SQL block with “/”
and include a SELECT statement to display the department that you updated.
END;
/
SELECT * FROM departments WHERE department_id=280;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Practices for Lesson
I G 6: ens
O
Writing Control S T e lic
Structures
P era O bl
Chapter 6 UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
In this practice, you create PL/SQL blocks that incorporate loops and conditional control
structures. This practice tests your understanding of various IF statements and LOOP
constructs.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
1. Execute the command in the lab_06_01.sql file to create the messages table. Write a
PL/SQL block to insert numbers into the messages table.
a. Insert the numbers 1 through 10, excluding 6 and 8.
b. Commit before the end of the block.
c. Execute a SELECT statement to verify that your PL/SQL block worked.
Result: You should see the following output:
u se
to
G O e n se
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
2. Execute the lab_06_02.sql c This
IO ailฺscript. u e creates an emp table that is a replica of the
dscript
employeesJ
L
U It alters
table. m the emp t
S to add a new column, stars, of VARCHAR2 data
table
@
type and size 50. Createg a t h
PL/SQLi s block that inserts an asterisk in the stars column for
every $1000
2
3of8 an employee’s salary. Save your script as lab_06_02_soln.sql.
a. g e z
Inuthe declarative section of the block, declare a variable v_empno of type
r i
d emp.employee_id and initialize it to 176. Declare a variable v_asterisk of type
r o
(j ulio b.
emp.stars and initialize it to NULL. Create a variable v_sal of type emp.salary.
In the executable section, write logic to append an asterisk (*) to the string for every
$1,000 of the salary. For example, if the employee earns $8,000, the string of asterisks
should contain eight asterisks. If the employee earns $12,500, the string of asterisks
should contain 13 asterisks (rounded to the nearest whole number).
c. Update the stars column for the employee with the string of asterisks. Commit before
the end of the block.
d. Display the row from the emp table to verify whether your PL/SQL block has executed
successfully.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
BEGIN
FOR i in 1..10 LOOP
IF i = 6 or i = 8 THEN
null;
ELSE
INSERT INTO messages(results)
VALUES (i);
END IF;
END LOOP;
COMMIT;
END;
/ u se
to
G O e nse
c.
TI e lic
Execute a SELECT statement to verify that your PL/SQL block worked.
S
SELECT * FROM messages; Z PO erabl
U E nsf
Result: You should see the following output: R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
ioro
(jul
2. Execute the lab_06_02.sql script. This script creates an emp table that is a replica of the
employees table. It alters the emp table to add a new column, stars, of VARCHAR2 data
type and size 50. Create a PL/SQL block that inserts an asterisk in the stars column for
every $1000 of the employee’s salary. Save your script as lab_06_02_soln.sql.
a. In the declarative section of the block, declare a variable v_empno of type
emp.employee_id and initialize it to 176. Declare a variable v_asterisk of type
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
DECLARE
v_empno emp.employee_id%TYPE := 176;
v_asterisk emp.stars%TYPE := NULL;
v_sal emp.salary%TYPE;
b. In the executable section, write logic to append an asterisk (*) to the string for every
$1,000 of the salary. For example, if the employee earns $8,000, the string of asterisks
should contain eight asterisks. If the employee earns $12,500, the string of asterisks
should contain 13 asterisks.
u se
to
BEGIN
G O e n se
SELECT NVL(ROUND(salary/1000), 0) INTO v_sal
S TI e lic
PO erabl
FROM emp WHERE employee_id = v_empno;
Z
E nsf
FOR i IN 1..v_sal
LOOP
U
IG -tra
v_asterisk := v_asterisk R
D non
||'*';
O
R R has a uideฺ
END LOOP;
A
C
c. Update the stars column ESforothe
m ) ntwith
employee
G the string of asterisks. Commit before
L
the end of the O ailฺc tude
Iblock.
JU gm his S
2 t = v_asterisk
@ SET stars
UPDATE8 emp
z3 employee_id = v_empno;
u e
WHERE
g COMMIT;
o driEND;
j u l ior /
(
d. Display the row from the emp table to verify whether your PL/SQL block has executed
successfully.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Practices for Lesson
I G 7: ens
O
Working with O ST ble licData
Composite
P
Types EZ sfera
I G U ran
R 7 on-t
Chapter
D
O an
A R has uideฺ
R
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Note: If you have executed the code examples for this lesson, make sure you execute the
following code before starting this practice:
DROP table retired_emps;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
e.
TI e lic
You may want to execute and test the PL/SQL block for countries with the IDs DE, UK,
and US. S
2. PO erabl
Create a PL/SQL block to retrieve the names of some departments from the DEPARTMENTS
Z
U E nsf
table and print each department name on the screen, incorporating an associative array.
IG -tra
Save the script as lab_07_02_soln.sql.
R
O D non
a. Declare an INDEX BY table dept_table_type of type
R R has a uideฺ
departments.department_name. Declare a variable my_dept_table of type
A
ES om) nt G
dept_table_type to temporarily store the names of the departments.
C
IO ailฺc tude
b. Declare two variables: f_loop_count and v_deptno of type NUMBER. Assign 10 to
L
JU gm his S
f_loop_count and 0 to v_deptno.
2 @ t
8
g u ez3
r i
i orod
(jul
c. Using a loop, retrieve the names of 10 departments and store the names in the
associative array. Start with department_id 10. Increase v_deptno by 10 for every
loop iteration. The following table shows the department_id for which you should
retrieve the department_name.
DEPARTMENT_ID DEPARTMENT_NAME
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
50 Shipping
60 IT
70 Public Relations
80 Sales
u se
90 Executive to
G O e n se
100
S TI e lic Finance
d. Using another loop, retrieve the department information from the associative array and
display the information.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
SET SERVEROUTPUT ON
c. In the declarative section, use the %ROWTYPE attribute and declare the
v_country_record variable of type countries.
v_country_record countries%ROWTYPE;
d. In the executable section, get all the information from the COUNTRIES table by using u se
to
v_countryid. Display selected information about the country.
G O e nse
S TI e lic
PO erabl
BEGIN
SELECT *
Z
E nsf
INTO v_country_record U
IG -tra
FROM countries
WHERE country_id = UPPER(v_countryid); R
D non
O
DBMS_OUTPUT.PUT_LINE ('Country A R R hId: a s a' ||uideฺ
ES om) ||nt G
v_country_record.country_id
C
IO ' ||a'il||
' Country Name:
L t u de
ฺc v_country_record.country_name
JU gm his S
|| ' Region: v_country_record.region_id);
2 @ t
END;
z3 8
u e
r
The
d igsample output after performing all the above steps is as follows:
ioro
(jul
e. You may want to execute and test the PL/SQL block for countries with the IDs DE, UK,
and US.
2. Create a PL/SQL block to retrieve the names of some departments from the DEPARTMENTS
table and print each department name on the screen, incorporating an associative array.
Save the script as lab_07_02_soln.sql.
a. Declare an INDEX BY table dept_table_type of type
departments.department_name. Declare a variable my_dept_table of type
dept_table_type to temporarily store the names of the departments.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
SET SERVEROUTPUT ON
DECLARE
TYPE dept_table_type is table of
departments.department_name%TYPE
INDEX BY PLS_INTEGER;
my_dept_table dept_table_type;
e z 38 20 Marketing
r i g u 30 Purchasing
o d
j u l ior 40 Human Resources
( 50 Shipping
60 IT
70 Public Relations
80 Sales
90 Executive
100 Finance
BEGIN
FOR i IN 1..f_loop_count
LOOP
v_deptno:=v_deptno+10;
SELECT department_name
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
INTO my_dept_table(i)
FROM departments
WHERE department_id = v_deptno;
END LOOP;
d. Using another loop, retrieve the department names from the associative array and
display them.
FOR i IN 1..f_loop_count
LOOP
DBMS_OUTPUT.PUT_LINE (my_dept_table(i));
END LOOP;
END;
u se
e. Execute and save your script as lab_07_02_soln.sql. The output is as follows: to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z 3
3. Modify the block 8 that you created in Practice 2 to retrieve all information about each
g u e
department from the DEPARTMENTS table and display the information. Use an associative
r i
darray with the INDEX BY table of records method.
r o
(j ulio a. Load the lab_07_02_soln.sql script.
b. You have declared the associative array to be of the
departments.department_name type. Modify the declaration of the associative
array to temporarily store the number, name, and location of all the departments. Use
the %ROWTYPE attribute.
SET SERVEROUTPUT ON
DECLARE
TYPE dept_table_type is table of departments%ROWTYPE
INDEX BY PLS_INTEGER;
my_dept_table dept_table_type;
f_loop_count NUMBER (2):=10;
v_deptno NUMBER (4):=0;
c. Modify the SELECT statement to retrieve all department information currently in the
DEPARTMENTS table and store it in the associative array.
BEGIN
FOR i IN 1..f_loop_count
LOOP
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
d. Using another loop, retrieve the department information from the associative array and
display the information.
FOR i IN 1..f_loop_count
LOOP
u se
DBMS_OUTPUT.PUT_LINE ('Department Number: ' ||
to
my_dept_table(i).department_id
G O e nse
TI e lic
|| ' Department Name: ' || my_dept_table(i).department_name
|| ' Manager Id: '|| my_dept_table(i).manager_idS
PO erabl
|| ' Location Id: ' || my_dept_table(i).location_id);
Z
END LOOP;
U E nsf
END;
R IG -tra
O D non
R R has a uideฺ
The sample output is as follows:
A
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
l i o ro
(j u
u se
e to
Practices for Lesson
I G O
8: Using
c e ns
T li
P era OS ble
Explicit Cursors
Chapter 8 UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
• Second, you write a PL/SQL block that processes information with two cursors,
including one that uses a parameter.
1. Create a PL/SQL block to perform the following:
a. In the declarative section, declare and initialize a variable named v_deptno of type
NUMBER. Assign a valid department ID value (see table in step d for values).
b. Declare a cursor named c_emp_cursor, which retrieves the last_name, salary,
and manager_id of employees working in the department specified in v_deptno.
c. In the executable section, use the cursor FOR loop to operate on the data retrieved. If
the salary of the employee is less than 5,000 and if the manager ID is either 101 or
124, display the message “<<last_name>> Due for a raise.” Otherwise, display the
message “<<last_name>> Not Due for a raise.”
u se
d. Test the PL/SQL block for the following cases: to
G O e nse
Department ID Message S TI e lic
Whalen Due for a raiseZ P
O abl
E nsfer
10
U ara
20 Hartstein Not Due
Fay Not Due D RIaGfor n - t raise
naoraise
for raise
O
50 Weiss Not
A R R
a s a
Due for
d eฺ
for auiraise
Fripp
E S ) h Duet G
Not Due
CKaufling
c o m Not
e n for a raise
L IO .a.ilฺ. tud
Vollman Not Due for a raise. . .
JU gmOConnell h i s S Due for a raise
2 @ Grantt Due for a raise
80ez3
8 Russell Not Due for a raise
g u
dri
Partners Not Due for a raise
2. Next, write a PL/SQL block that declares and uses two cursors—one without a parameter
and one with a parameter. The first cursor retrieves the department number and the
department name from the DEPARTMENTS table for all departments whose ID number is
less than 100. The second cursor receives the department number as a parameter, and
retrieves employee details for those who work in that department and whose
employee_id is less than 120.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
U E nsf
R IG -tra
Notes
O D non
• Check whether c_emp_cursor
R R s a open dbefore
is already
e ฺ opening the cursor.
SA attribute
• Use the appropriate cursor hafor the i
ucondition.
exit
E m )
C ฺcoprint a line t G
n you have displayed the details of each
• When the loop completes,
I O i l u d eafter
L
JU gma his St
department, and close c_emp_cursor.
2 @ t
z3 8
f. End the first loop and close c_dept_cursor. Then end the executable section.
u e
d r ig
ioro
(jul
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
DECLARE
v_deptno NUMBER := 10;
Department ID Message
10 Whalen Due for a raise
20 Hartstein Not Due for a raise
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
e z 38
by department_id.
r i g u
od CURSOR c_dept_cursor IS
DECLARE
i o r
(j ul SELECT department_id,department_name
FROM departments
WHERE department_id < 100
ORDER BY department_id;
SELECT last_name,job_id,hire_date,salary
FROM employees
WHERE department_id = v_deptno
AND employee_id < 120;
c. Declare variables to hold the values retrieved from each cursor. Use the %TYPE
attribute while declaring variables.
v_current_deptno departments.department_id%TYPE;
v_current_dname departments.department_name%TYPE;
v_ename employees.last_name%TYPE;
u se
v_job employees.job_id%TYPE; to
v_hiredate employees.hire_date%TYPE;
G O e n se
v_sal employees.salary%TYPE;
S TI e lic
Z PO erabl
d. Open c_dept_cursor and use a simple loop toU
E nsinto
fetch values
f the variables
declared. Display the department numberR IGdepartment
and
n - t ra name. Use the appropriate
cursor attribute to exit the loop.
R OD a no ฺ
A R has uide
BEGIN
C ES om) nt G
LOOP L IO ailฺc tude
OPEN c_dept_cursor;
JU c_dept_cursor
FETCH g m is S INTO v_current_deptno,
2 @
v_current_dname;th
z 8 WHEN c_dept_cursor%NOTFOUND;
3EXIT
g u e DBMS_OUTPUT.PUT_LINE ('Department Number : ' ||
d r i v_current_deptno || ' Department Name : ' ||
io r o v_current_dname);
(jul
Notes
• Check whether c_emp_cursor is already open before opening the cursor.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
IF c_emp_cursor%ISOPEN THEN
CLOSE c_emp_cursor;
END IF;
OPEN c_emp_cursor (v_current_deptno);
LOOP
FETCH c_emp_cursor INTO v_ename,v_job,v_hiredate,v_sal;
se
EXIT WHEN c_emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (v_ename || ' ' || v_job
to u
se
|| ' ' || v_hiredate || ' ' || v_sal);
END LOOP;
G O e n
S
DBMS_OUTPUT.PUT_LINE('--------------------------------------------TI e lic
--------------------------------------------');
Z PO erabl
CLOSE c_emp_cursor;
U E nsf
R IG -tra
O D non
R R has a Then
f. End the first loop and close c_dept_cursor.
A i d eฺ the executable section.
end
E S ) G u
END LOOP; O C o m en t
L I i l ฺ c u d
JU gma his St
CLOSE c_dept_cursor;
END;
2 @ t
z3 8
u e
d r ig
ioro
(jul
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
2. In the declarative section, declare the v_num variable of the NUMBER type that holds a
number n, representing the number of top n earners from the employees table. For
example, to view the top five salaries, enter 5. Declare another variable sal of type
employees.salary. Declare a cursor, c_emp_cursor, which retrieves the salaries of
employees in descending order. Remember that the salaries should not be duplicated.
3. In the executable section, open the loop and fetch the top n salaries, and then insert them
into the TOP_SALARIES table. You can use a simple loop to operate on the data. Also, try
and use the %ROWCOUNT and %FOUND attributes for the exit condition.
Note: Make sure that you add an exit condition to avoid having an infinite loop.
4. After inserting data into the TOP_SALARIES table, display the rows with a SELECT
statement. The output shown represents the five highest salaries in the EMPLOYEES table.
u se
to
G O e n se
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
5. Test a variety of special cases such
A RasRv_num
a s =a
i d
0 or whereeฺv_num is greater than the
S
number of employees in the EMPLOYEES
E ) htable. Empty
G uthe TOP_SALARIES table after each
test. C ฺco m en t
L I O i l u d
JU gma his St
2 @ t
z3 8
u e
d r ig
l i o ro
(j u
1. Execute the lab_08-02.sql script to create a new table, TOP_SALARIES, for storing the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
DECLARE
v_num NUMBER(3) := 5;
v_sal employees.salary%TYPE;
CURSOR c_emp_cursor IS
SELECT salary
u se
FROM employees
to
ORDER BY salary DESC;
G O e nse
T I l ic them
S
3. In the executable section, open the loop and fetch the top n salaries, and thene insert
bldata. Also, try
PO eonrathe
into the TOP_SALARIES table. You can use a simple loop to operate
Z
and use the %ROWCOUNT and %FOUND attributes for the U E s f
exit condition.
n
Note: Make sure that you add an exit conditionRtoIG - t ra an infinite loop.
avoid having
n
R OD a no ฺ
BEGIN R has uide
OPEN c_emp_cursor; SA ) G
FETCH c_emp_cursor C EINTO om v_sal;ent
L IO ailฺc tu<=d v_num AND c_emp_cursor%FOUND LOOP
WHILE c_emp_cursor%ROWCOUNT
JU INSERT
g h
S
m INTOistop_salaries (salary)
2 @
VALUES t
(v_sal);
z 8
FETCH 3c_emp_cursor INTO v_sal;
e
ENDuLOOP;
d r ig c_emp_cursor;
CLOSE
ioroEND;
(jul 4. After inserting data into the TOP_SALARIES table, display the rows with a SELECT
statement. The output shown represents the five highest salaries in the EMPLOYEES table.
/
SELECT * FROM top_salaries;
5. Test a variety of special cases such as v_num = 0 or where v_num is greater than the
number of employees in the EMPLOYEES table. Empty the TOP_SALARIES table after each
test.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Practices for Lesson
I G 9: ens
O
S T e lic
Handling Exceptions
P era O bl
Chapter 9 UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
In this practice, you write a PL/SQL block that applies a predefined exception to process only
one record at a time. The PL/SQL block selects the name of the employee with a given salary
value.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
1. Execute the command in the lab_06_01.sql file to re-create the messages table.
2. In the declarative section, declare two variables: v_ename of type
employees.last_name and v_emp_sal of type employees.salary. Initialize the latter
to 6000.
3. In the executable section, retrieve the last names of employees whose salaries are equal to
the value in v_emp_sal. If the salary entered returns only one row, insert into the
MESSAGES table the employee’s name and the salary amount.
Note: Do not use explicit cursors.
4. If the salary entered does not return any rows, handle the exception with an appropriate
exception handler and insert into the MESSAGES table the message “No employee with a
salary of <salary>.”
5. If the salary entered returns multiple rows, handle the exception with an appropriate u se
exception handler and insert into the MESSAGES table the message “More than one to
employee with a salary of <salary>.” G O e nse
6. S TI e lic
Handle any other exception with an appropriate exception handler and insert into the
PO erabl
MESSAGES table the message “Some other error occurred.”
Z
7. U E nsf
Display the rows from the MESSAGES table to check whether the PL/SQL block has
IG -tra
executed successfully. The output is as follows:
R
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gvalue h i s S
m of v_emp_sal
@
8. Change the initialized
2 t to 2000 and re-execute. The output is as
follows:
e z 38
r i g u
i o r od
(j ul
1. Execute the command in the lab_06_01.sql file to re-create the MESSAGES table.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
DECLARE
v_ename employees.last_name%TYPE;
v_emp_sal employees.salary%TYPE := 6000;
3. In the executable section, retrieve the last names of employees whose salaries are equal to
the value in v_emp_sal. If the salary entered returns only one row, insert the employee’s
name and the salary amount into the MESSAGES table.
u se
Note: Do not use explicit cursors. to
G O e n se
BEGIN
S TI e lic
SELECT last_name
Z PO erabl
INTO
FROM
v_ename
employees U E nsf
WHERE salary = v_emp_sal; RIG
n - t ra
INSERT INTO messages (results)
VALUES (v_ename || ' - ' ||Rv_emp_sal);
OD a no ฺ
A R has uide
C ES om ) nhandle
t G
4. If the salary entered does
I O not return
i l ฺ c u d e employee
any rows, the exception with an appropriate
L
JU gma his St
exception handler and insert the message “No with a salary of <salary>” into the
MESSAGES table.
2 @ t
z3 8
EXCEPTION
g u e
r i
WHEN no_data_found THEN
d INSERT INTO messages (results)
ioro VALUES ('No employee with a salary of '||
(jul TO_CHAR(v_emp_sal));
5. If the salary entered returns multiple rows, handle the exception with an appropriate
exception handler and insert the message “More than one employee with a salary of
<salary>” into the MESSAGES table.
6. Handle any other exception with an appropriate exception handler and insert the message
“Some other error occurred” into the MESSAGES table.
END;
7. Display the rows from the MESSAGES table to check whether the PL/SQL block has
executed successfully.
/
SELECT * FROM messages;
u se
to
G O e nse
S TI e lic
PO eThe
8. Change the initialized value of v_emp_sal to 2000 and re-execute. r a bl is as
output
follows:
Z
E nsf
U
IG -tra
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
l i o ro
(j u
2. In the executable section, display “Deleting department 40....” Include a DELETE statement
to delete the department with the department_id 40.
3. Include an exception section to handle the e_childrecord_exists exception and
display the appropriate message.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
SET SERVEROUTPUT ON
DECLARE
e_childrecord_exists EXCEPTION;
PRAGMA EXCEPTION_INIT(e_childrecord_exists, -02292);
2. In the executable section, display “Deleting department 40....” Include a DELETE statement
to delete the department with department_id 40.
BEGIN
DBMS_OUTPUT.PUT_LINE(' Deleting department 40........');
u se
delete from departments where department_id=40;
to
G O e nse
3. Include an exception section to handle the e_childrecord_exists I
Texception
l icand
S e
display the appropriate message.
Z PO erabl
U E nsf
EXCEPTION
WHEN e_childrecord_exists THEN RI
G -tra
O n department. There are
Ddeletenothis
DBMS_OUTPUT.PUT_LINE(' Cannot
R ฺ
R(childs arecordsdeexist.)
employees in this department
A h a u i ');
END;
C ES om) nt G
O
asI follows:ail
The sample output is L
ฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
ioro
(jul
u se
e to
Practices for Lesson I G 10: ens
O
S
Introducing Stored
T e lic
P O abl
ProceduresZ
E and r
G U ran sfeFunctions
I
R 10 on-t
Chapter
D
O an
A R has uideฺ
R
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Note: If you have executed the code examples for this lesson, make sure you execute the
following code before starting this practice:
DROP table dept;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
DECLARE
v_today DATE:=SYSDATE;
v_tomorrow v_today%TYPE;
se
BEGIN
v_tomorrow:=v_today +1;
to u
se
DBMS_OUTPUT.PUT_LINE(' Hello World ');
DBMS_OUTPUT.PUT_LINE('TODAY IS : '|| v_today);
G O e n
DBMS_OUTPUT.PUT_LINE('TOMORROW IS : ' || v_tomorrow);
S TI e lic
END;
Z PO erabl
U E nsf
R n ra
IG to a-tprocedure
a. Modify the script to convert the anonymous block called greet.
OD aONncommand.)
(Hint: Also remove the SET SERVEROUTPUT
R o ฺ
A Rprocedure.
b. Execute the script to create the
h a u i deresults should be as follows:
s The output
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
o dc.rigSave this script as lab_10_01_soln.sql.
j u l ior d. Click the Clear button to clear the workspace.
( e. Create and execute an anonymous block to invoke the greet procedure.
(Hint: Ensure that you enable SERVEROUTPUT at the beginning of the block.)
p_name.
c. Print Hello <name> (that is, the contents of the argument) instead of printing Hello
World.
d. Save your script as lab_10_02_soln.sql.
e. Execute the script to create the procedure. The output results should be as follows:
u se
f. Create and execute an anonymous block to invoke the greet procedure with a to
parameter value. The block should also produce the output. G O e nse
S TI e lic
The sample output should be similar to the following:
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
d r ig
l i o ro
(j u
DECLARE
v_today DATE:=SYSDATE;
v_tomorrow v_today%TYPE;
BEGIN
v_tomorrow:=v_today +1;
DBMS_OUTPUT.PUT_LINE(' Hello World ');
DBMS_OUTPUT.PUT_LINE('TODAY IS : '|| v_today);
DBMS_OUTPUT.PUT_LINE('TOMORROW IS : ' || v_tomorrow);
END;
a. Modify the script to convert the anonymous block to a procedure called greet. (Hint:
u se
Also remove the SET SERVEROUTPUT ON command.)
to
G O e n se
CREATE PROCEDURE greet IS
S TI e lic
V_today DATE:=SYSDATE;
Z PO erabl
...
V_tomorrow today%TYPE;
U E nsf
R IG -tra
O
b. Execute the script to create the procedure. n n results should be as follows:
D The output
o
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
z3 8
u e
g this script as lab_10_01_soln.sql.
c. riSave
d
iorod. Click the Clear button to clear the workspace.
(jul e. Create and execute an anonymous block to invoke the greet procedure. (Hint: Ensure
that you enable SERVEROUTPUT at the beginning of the block.)
SET SERVEROUTPUT ON
BEGIN
greet;
END;
b. Modify the procedure to accept an argument of type VARCHAR2. Call the argument
p_name. u se
to
G O e nse
CREATE PROCEDURE greet(p_name VARCHAR2) IS
S TI e lic
PO erabl
V_today DATE:=SYSDATE;
V_tomorrow today%TYPE;
Z
E nsf
c. Print Hello <name> instead of printing Hello
U
IGWorld.-tra
R
D non
O
R s a deฺ
BEGIN
V_tomorrow:=v_todayS+1; A R h a Gui
C E oHello )
m '|| n tp_name);
DBMS_OUTPUT.PUT_LINE('
c e
...
U LIO mailฺ Stud
J g this
2
d. Save your script
8 @ as lab_10_02_soln.sql.
e
e. Execute z 3the script to create the procedure. The output results should be as follows:
ri g u
o d
ior
(jul
f. Create and execute an anonymous block to invoke the greet procedure with a
parameter value. The block should also produce the output.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices
I G O ens
and
S T 1e lic
Solutions for Lesson
P era O bl
Chapter 11UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Practices Overview
There are no practices for this lesson.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices
I G O ens
and
S T 2e lic
Solutions for Lesson
P era O bl
Chapter 12UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Overview
These additional practices are provided as a supplement to the Oracle Database: PL/SQL
Fundamentals course. In these practices, you apply the concepts that you learned in the course.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Overview
These paper-based exercises are used for extra practice in declaring variables and writing
executable statements.
Evaluate each of the following declarations. Determine which of them are not legal and explain
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
why.
1. DECLARE
name,dept VARCHAR2(14);
2. DECLARE
test NUMBER(5);
3. DECLARE
MAXSALARY NUMBER(7,2) = 5000;
4. DECLARE
JOINDATE BOOLEAN := SYSDATE;
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
2. DECLARE
test NUMBER(5);
This is legal.
3. DECLARE
MAXSALARY NUMBER(7,2) = 5000;
This is illegal because the assignment operator is wrong. It should be :=.
4. DECLARE
JOINDATE BOOLEAN := SYSDATE;
This is illegal because there is a mismatch in the data types. A Boolean data type
cannot be assigned a date value. The data type should be date.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices
I G O ens
and
S T 3e lic
Solutions for Lesson
P era O bl
Chapter 13UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
4. test := FALSE;
6. var := sysdate;
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Date
3. sal := (1000*12) + 500
Number
4. test := FALSE;
Boolean
5. temp := temp1 < (temp2/ 3);
Boolean
6. var := sysdate;
Date
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices
I G O ens
and
S T 4e lic
Solutions for Lesson
P era O bl
Chapter 14UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
DECLARE
v_custid NUMBER(4) := 1600;
v_custname VARCHAR2(300) := 'Women Sports Club';
v_ new_custid NUMBER(3) := 500;
BEGIN
DECLARE
v_custid NUMBER(4) := 0;
v_custname VARCHAR2(300) := 'Shape up Sports Club';
v_new_custid NUMBER(3) := 300;
v_new_custname VARCHAR2(300) := 'Jansports Club';
BEGIN
v_custid := v_new_custid;
v_custname := v_custname || ' ' || v_new_custname;
u se
1
to
END;
G O e nse
TI e lic
v_custid := (v_custid *12) / 10;
2 S
END;
Z PO erabl
U s f
E and ndata
Evaluate the preceding PL/SQL block and determine Ithe
R G -tra type of each of the
value
D non
following variables, according to the rules of scoping:
O
1. v_custid at position 1:
A R R has a uideฺ
S ) nt G
2. v_custname at positionC 1:E o m
L IO ailฺc tude
JU at position
g m 1: is S
3. v_new_custid
2 @ th
e z 38
g u
4. v_new_custname
r i at position 1:
d
ioro
(jul 5. v_custid at position 2:
6. v_custname at position 2:
DECLARE
v_custid NUMBER(4) := 1600;
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
z
4. v_new_custname
e 38 at position 1:
g u Club, and the data type is VARCHAR2.
Jansports
r i
rodv_custid at position 2:
io5.
(jul 1920, and the data type is NUMBER.
6. v_custname at position 2:
Women Sports Club, and the data type is VARCHAR2.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices
I G O ens
and
S T 5e lic
Solutions for Lesson
P era O bl
Chapter 15UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Key Type
Nulls/Unique
FK Table
FK Column
Data Type Number VARCHAR2 Date
Length 7,2 35
Key Type
Nulls/Unique
FK Table
FK Column
Data Type Number VARCHAR2 Date
Length 7,2 35
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices
I G O ens
and
S T 6e lic
Solutions for Lesson
P era O bl
Chapter 16UE
Z s f
IG -tra n
R
D non
O
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
2. Test your solution by using the following table. For example, if the year entered is 1990, the
output should be “1990 is not a leap year.”
1990 Not a leap year
2000 Leap year
1996 Leap year
1886 Not a leap year
1992 Leap year
1824 Leap year
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
DECLARE
v_YEAR NUMBER(4) := &P_YEAR;
v_REMAINDER1 NUMBER(5,2);
v_REMAINDER2 NUMBER(5,2);
v_REMAINDER3 NUMBER(5,2);
BEGIN
v_REMAINDER1 := MOD(v_YEAR,4);
v_REMAINDER2 := MOD(v_YEAR,100);
v_REMAINDER3 := MOD(v_YEAR,400);
IF ((v_REMAINDER1 = 0 AND v_REMAINDER2 <> 0 ) OR
v_REMAINDER3 = 0) THEN
DBMS_OUTPUT.PUT_LINE(v_YEAR || ' is a leap year');
u se
ELSE
to
DBMS_OUTPUT.PUT_LINE(v_YEAR || ' is not a leap
G O e nse
year');
S TI e lic
PO erabl
END IF;
END;
Z
E nsf
/ U
IGexample, rifathe year entered is 1990, the
2. Test your solution by using the following table.R
For
n - t
OD a no ฺ
output should be “1990 is not a leap year.”
R
1990 Not a leap year R
A h a s uide
2000 Leap year ES ) nt G
C o m
1996
1886
Leap year
L
Not IaOleap year
a i lฺc tude
1992 JULeap year
g m is S
1824 2 @
Leap year th
e z 38
r i g u
i o r od
(j ul
u se
2. Test the PL/SQL for the following test cases: to
Monthly Salary Bonus G O e nse
3000 2000 S TI e lic
1200 1000
Z PO erabl
800 500
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
to
G O e nse
SET SERVEROUTPUT ON;
S TI e lic
DECLARE
Z PO erabl
V_SAL
E nsf
NUMBER(7,2) := &B_SALARY;
U
V_BONUS NUMBER(7,2);
V_ANN_SALARY NUMBER(15,2); RI
G -tra
O D non
R R* h12;s a uideฺ
BEGIN
V_ANN_SALARY := V_SAL
A
S>= 20000 a
) THEN G
IF V_ANN_SALARY
C E o m n t
ude AND V_ANN_SALARY >=10000 THEN
LIO mailฺc <=St19999
V_BONUS := 2000;
ELSIFUV_ANN_SALARY
J
V_BONUS
ELSE 82@
this
g:= 1000;
e 3
zV_BONUS := 500;
g u
o dri END IF;
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices I G O ens
and
S
Solutions for Lesson
T 7: e l ic
P O abl
U
Z s f er
WorkingEwith Composite Data
n
D non RIG -tra
Types
O
R s a 17 deฺ
Chapter
R
A ) ha Gui
S
E om t
C c e n
U LIO mailฺ Stud
J g this
2 @
z 38
r i g ue
o d
j u l ior
(
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 1
Edutronic Global Services EฺIฺRฺL
Overview
In the following exercises, you practice using associative arrays (this topic is covered in the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
lesson titled “Working with Composite Data Types”) and explicit cursors (this topic is covered in
the lesson titled “Using Explicit Cursors”). In the first exercise, you define and use an explicit
cursor to fetch data. In the second exercise, you combine the use of associative arrays with an
explicit cursor to output data that meets a certain criteria.
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 2
Edutronic Global Services EฺIฺRฺL
is later than 01-FEB-1988, display the employee name, salary, and hire date in the format
shown in the following sample output:
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 3
Edutronic Global Services EฺIฺRฺL
DECLARE
CURSOR C_EMP_CUR IS
SELECT last_name,salary,hire_date FROM EMPLOYEES;
V_ENAME VARCHAR2(25);
v_SAL NUMBER(7,2);
V_HIREDATE DATE;
2. Process each row from the cursor, and if the salary is greater than 15,000 and the hire date
is later than 01-FEB-1988, display the employee name, salary, and hire date in the format
shown in the following sample output:
BEGIN
OPEN C_EMP_CUR;
FETCH C_EMP_CUR INTO V_ENAME,V_SAL,V_HIREDATE; u se
WHILE C_EMP_CUR%FOUND to
LOOP
G O e nse
IF V_SAL > 15000 AND V_HIREDATE >=
S TI e lic
Z PO erabl
TO_DATE('01-FEB-1988','DD-MON-YYYY') THEN
DBMS_OUTPUT.PUT_LINE (V_ENAME || ' earns '
U E nsf
|| TO_CHAR(V_SAL)|| ' and joined the organization on '
R IG -tra
|| TO_DATE(V_HIREDATE,'DD-Mon-YYYY'));
END IF;
O D non
R R has a uideฺ
FETCH C_EMP_CUR INTO V_ENAME,V_SAL,V_HIREDATE;
A
ES om) nt G
END LOOP;
CLOSE C_EMP_CUR;
C
END;
L IO ailฺc tude
/
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 4
Edutronic Global Services EฺIฺRฺL
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 5
Edutronic Global Services EฺIฺRฺL
r i g u
LOOP
i orod i := i +1;
(jul
Tename(i) := emprec.last_name;
Tdept(i) := emprec.department_id;
DBMS_OUTPUT.PUT_LINE ('Employee: ' || Tename(i) ||
' is in department number: ' || Tdept(i));
END LOOP;
END;
/
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 6
Edutronic Global Services EฺIฺRฺL
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 7
Edutronic Global Services EฺIฺRฺL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Additional Practices and Solutions for Lesson 7: Working with Composite Data Types
Chapter 17 - Page 8
Edutronic Global Services EฺIฺRฺL
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
e to
Additional Practices I G O ens
and
S T 8:
Solutions for Lesson e l ic
Using
P O abl
Z
Explicit Cursors r
G U ran E sfe
I
R 18 on-t
Chapter
D
O an
A R has uideฺ
R
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
Practices Overview
Practices of this lesson are included in Practice 7.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
u se
e to
Additional Practices I G O ens
and
S T 9:
Solutions for Lesson e l ic
P O abl
Z
HandlingEExceptionsr
G U ran sfe
I
R 19 on-t
Chapter
D
O an
A R has uideฺ
R
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul
In this practice, you write a PL/SQL block that handles an exception, as follows:
1. Declare variables for the employee last name, salary, and hire date. Use a substitution
variable for the employee last name. Then, query the EMPLOYEES table for the
last_name, salary, and hire_date of the specified employee.
2. If the employee has been with the organization for more than five years, and if that
employee’s salary is less than 3,500, raise an exception. In the exception handler, perform
the following:
• Output the following information: employee last name and the message “due for a
raise,” similar to the following:
u se
to
G O e nse
S TI e lic
Z PO erabl
• Insert the last name, years of service, and salary Uinto n f table.
E the ANALYSIS
s
R G -tra
Iname
3. If there is no exception, output the employee
O D nonand the message “not due for a
last
R R has a uideฺ
raise,” similar to the following:
A
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
3 by querying the ANALYSIS table. Use the following test cases to test the
zresults
4. Verify the
u e
d r ig block.
PL/SQL
l i o ro LAST_NAME MESSAGE
(j u Austin
Nayer
Not due for a raise
Due for a raise
Fripp Not due for a raise
Khoo Due for a raise
In this practice, you write a PL/SQL block that handles an exception, as follows:
1. Declare variables for the employee last name, salary, and hire date. Use a substitution
variable for the employee last name. Then, query the EMPLOYEES table for the
last_name, salary, and hire_date of the specified employee.
2. If the employee has been with the organization for more than five years, and if that
employee’s salary is less than 3,500, raise an exception. In the exception handler, perform
the following:
• Output the following information: employee last name and the message “due for a
raise.”
u se
• Insert the employee name, years of service, and salary into the ANALYSIS table. to
O nse
3. If there is no exception, output the employee last name and the message “not due for a
G e
raise.”
S TI e lic
SET SERVEROUTPUT ON;
Z PO erabl
DECLARE
U E nsf
E_DUE_FOR_RAISE EXCEPTION;
R IG -tra
O D non
V_HIREDATE EMPLOYEES.HIRE_DATE%TYPE;
R R has a uideฺ
V_ENAME EMPLOYEES.LAST_NAME%TYPE := INITCAP( '& B_ENAME');
A
ES om) nt G
V_SAL EMPLOYEES.SALARY%TYPE;
C
IO ailฺc tude
V_YEARS NUMBER(2);
L
BEGIN
JU gm his S
2 @ t
SELECT SALARY,HIRE_DATE,MONTHS_BETWEEN(SYSDATE,hire_date)/12
8
ez3
YEARS
r i g u
INTO V_SAL,V_HIREDATE,V_YEARS
(jul
IF V_SAL < 3500 AND V_YEARS > 5 THEN
RAISE E_DUE_FOR_RAISE;
ELSE
DBMS_OUTPUT.PUT_LINE (V_ENAME|| ' not due for a
raise');
END IF;
EXCEPTION
WHEN E_DUE_FOR_RAISE THEN
BEGIN
DBMS_OUTPUT.PUT_LINE (V_ENAME || ' due for a raise');
INSERT INTO ANALYSIS(ENAME,YEARS,SAL)
VALUES (V_ENAME,V_YEARS,V_SAL);
END;
END;
/
4. Verify the results by querying the ANALYSIS table. Use the following test cases to test the
PL/SQL block.
MESSAGE
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2014, Oracle and/or its affiliatesฺ
LAST_NAME
Austin Not due for a raise
Nayer Due for a raise
Fripp Not due for a raise
Khoo Due for a raise
u se
to
G O e nse
S TI e lic
Z PO erabl
U E nsf
R IG -tra
O D non
A R R has a uideฺ
C ES om) nt G
L IO ailฺc tude
JU gm his S
2 @ t
8
g u ez3
r i
i orod
(jul