You are on page 1of 4

Lab Assignment 10 – Section 10 Key

CIS 208A PL/SQL Programming and SQL

Name: _______________________________

Section 10 Lesson 1 Exercise #1, 2, 3, 4, 5,6, 7, 8, 9

1. Name at least four objects that can be put into a package.

2. Name the two components of a package and explain the difference between them.

3. Which of the two components must be created first, and why?

4. Write a query against a Data Dictionary view to show you a list of packages you own.

5. Create the specification for the check_emp_pkg which you studied in this lesson. The
specification should declare a constant and two procedures, as follows:

o g_max_length_of_service, datatype NUMBER, initialized to 100

o chk_hiredate with one input parameter having the same datatype as


employees.hire_date
o chk_dept_mgr with two input parameters having the same datatypes as
employees.employee_id and employees.manager_id.

6. Create the package body for check_emp_pkg. Remember that the names and
parameters of the procedures in the body must be identical to those in the
specification, or the body will not compile.

The code for chk_hiredate should RAISE_APPLICATION_ERROR if the employee was hired
more than 100 years ago (hint: use MONTHS_BETWEEN, comparing with
g_max_length_of_service * 12).

The second procedure, chk_dept_mgr, accepts two input parameters: an employee_id and a
manager_id. The code should find the manager of the employee’s department and check whether
this manager has the same manager_id as the second parameter. If the manager_id is the same,
Lab_10_Sp18_208A.doc
display a suitable “success” message; if they are different, raise an application error. Include an
exception handler for NO_DATA_FOUND.

The following sample data from the employees and departments tables may help you:
Departments:

Employees:

Passing parameters (174,149) would be successful, while (174,176) would raise an error.

7. Procedure chk_hiredate: Test the chk_hiredate procedure using input value 17-Jan-87 (it
should succeed).

8. Test the chk_dept_mgr procedure twice using input values (174,149) and
(174,176). The first should succeed while the second should fail.

9. Now you want to modify the package so that the chk_dept_mgr procedure displays a different
error message if the two manager_ids are different. What do you need to recreate: the
Specification, the Body, or both? Make the change and test it again, using (174,176) as before.

Section 10 Lesson 2 Exercise #1, 2, 3, 4

1. Create a package called hellofrom that contains three public procedures named proc_1, proc_2 and
proc_3. Each of these procedures should use DBMS_OUTPUT.PUT_LINE() to display the
message “Hello from Proc x” where “x” is 1 or 2 or 3 as appropriate.

Also, proc_1 should call proc_2 and proc_2 should call proc_3, so you need to include a
reference to proc_2 from proc_1, and a reference to proc_3 from proc_2.

Lab_10_Sp18_208A.doc
You will be making changes to this package specification and body, so make sure you save both
the specification and body in your Application Express session.

2. DESCRIBE the package to check that all three procedures are visible. Then, create and execute an
anonymous block which displays all three messages with only one procedure call.

3. Modify the hellofrom package specification (not the body) so that only proc_1 is public and
proc_2 and proc_3 are private. Then try to describe the package. What do you see?

4. What will happen if you try to run hellofrom.proc_1 now before you make any changes to the
body? Try it.

Section 10 Lesson 3 Exercise #1, 2, 3, 4, 5

1. Create a package called overload. The package should contain three procedures all called
what_am_i. The first procedure should accept a VARCHAR2 as an IN parameter, the second a
NUMBER and the third a DATE. Each procedure should display a simple message to show which
datatype was passed to it. For example, the first procedure could display “Here I am a Varchar2”.
Save your work for later. When you are done, describe the package.

2. Test the overload package by calling it and passing in a character string, a number and a date
respectively. You should see the different messages returned. – Note: you must use the TO_DATE
function to pass a value in as a date. Paste results.

3. Now modify the overload package to have a fourth procedure in it, again called what_am_i,
accepting two parameters: p_in NUMBER and p_in2 VARCHAR2. Test the new procedure to
verify that it works.

Lab_10_Sp18_208A.doc
4. Modify the overload package specification again to add a fifth procedure called what_am_i
with the same two IN parameters as the fourth procedure. Try to recreate the specification. What
happens and why?

5. Suppose you write a packaged function that returns a BOOLEAN value of TRUE or FALSE. Does
the BOOLEAN limit the possible places from which you can call the packaged function?

Lab_10_Sp18_208A.doc