2. in Your Own Words, List the Benefits of Subprograms

Lab Assignment 8
CIS 208A PL/SQL Programming and SQL

Section 8-1, Exercise #2, 3, 4, 5, 6

2. In your own words, list the benefits of subprograms.

3. In your own words, describe a stored procedure.

4. The remaining questions in this practice use a copy of the employees table. Create the copy by executing the following SQL statement:

CREATE TABLE employees_dup AS SELECT * from employees;

A. Use the code below to create a procedure in Application Express. Save the definition of your procedure in case you need to modify it later. In the “Save SQL” popup, name your saved work “My name change procedure.”

CREATE OR REPLACE PROCEDURE name_change IS
BEGIN

UPDATE employees_dup
SET first_name = 'Susan'
WHERE department_id = 60;
END name_change;

B. Execute the procedure by running the following anonymous block:

BEGIN
name_change

END;

C. SELECT from the table to check that the procedure has executed correctly and performed the UPDATE:


5. Create a second procedure named pay_raise which changes the salary of all employees in employees_dup to a new value of 30000. Execute the procedure from anonymous block, then SELECT from the table to check that procedure has executed correctly.

6. Retrieve your first name_change procedure by clicking on its name in the Saved SQL window. Modify the code to remove OR REPLACE from the CREATE statement, and introduce a deliberate error into the code, for example by misspelling a keyword: UPDAT employees_dup. Execute your code to recreate the procedure. What happens?

Section 8-2, Exercise #2,

2. Using the countries table:

A. Create a procedure that accepts a country_id as a parameter and displays the name of the country and its capitol city. Name your procedure get_country_info. Save your procedure definition for later use.

B. Execute your procedure from an anonymous block, using country_id 90.

C. Re-execute the procedure from the anonymous block, this time using country_id 95. What happens?

D. Retrieve your procedure code from Saved SQL and modify it to trap the NO_DATA_FOUND exception in an exception handler. Execute the modified procedure using country_id 95 again. Now what happens?

Section 8-3, Exercise #1, 2

1. Name the three modes for parameters and indicate which mode is the default mode. Which mode cannot be modified inside the procedure?


2. Procedures:

A. Create a procedure that receives a country_id as an IN parameter and returns the name and population of that country as OUT parameters. Include an exception handler to trap the NO_DATA_FOUND exception if the country does not exist. The procedure should not display the returned values; this will be done in the next step. Name your procedure find_area_pop. Save your code.

B. Test your procedure by creating and executing an anonymous block that invokes the procedure and displays the returned OUT values. Save your code. Run the block twice, with country_ids 2 (Canada) and 10 (does not exist).

C. Retrieve your procedure code and modify it to add a third OUT parameter which is the population density of the country, using the formula: density = (population / area). You will need to modify your SELECT statement to fetch the area column value into a local variable. Save your modified code.

D. Test your modifed procedure using country_id 2. You will need to modify your calling anonymous block to declare and pass a fourth actual parameter to receive the population density from the procedure. Save your code.

Lab_8_Sp17_208A.doc