You are on page 1of 3

ITE 407 ADVANCED DATABASES - Lab 4 PROCEDURES

Creating and Using PL/SQL Procedures

Objective

1. To demonstrate the creation of Procedures in PL/SQL


2. To demonstrate the use of Parameter Modes in PL/SQL Procedures

Background/Theory

Parameters are used to transfer data values to and from the calling environment and the procedure (or
subprogram). Parameters are declared in the subprogram header, after the name and before the
declaration section for local variables.
Parameters are subject to one of the three parameter-passing modes: IN, OUT, or IN OUT.

An IN parameter passes a constant value from the calling environment into the procedure.

An OUT parameter passes a value from the procedure to the calling environment.

An IN OUT parameter passes a value from the calling environment to the procedure and a
possibly different value from the procedure back to the calling environment using the
same parameter.

Parameters can be thought of as a special form of local variable, whose input values are initialized by
the calling environment when the subprogram is called, and whose output values are returned to the
calling environment when the subprogram returns control to the caller.

Tools/Equipment

The following resources will be required to successfully complete the tasks in this lab:

- Workstations running the Oracle client connected to an Oracle Server machine, or

- Workstations running the Oracle Express software.


Lab Activity

Using IN, OUT and IN OUT Parameters

Remember to enable SERVEROUTPUT if you have previously disabled it.

1. Create and invoke the ADD_JOB procedure and consider the results.

a. Create a procedure called ADD_JOB to insert a new job into the


JOBS table. Provide the ID and title of the job using two parameters.

b. Compile the code; invoke the procedure with IT_DBA as job ID and
Database Administrator as job title. Query the JOBS table to
view the results.

c. Invoke your procedure again, passing a job ID of ST_MAN and a job


title of Stock Manager. What happens and why?
____________________________________________________________

_____________________________________________________________

2. Create a procedure called GET_EMPLOYEE to query the EMPLOYEES table,


retrieving the salary and job ID for an employee when provided with the
employee ID.

a. Create a procedure that returns a value from the SALARY and JOB_ID
columns for a specified employee ID. Compile the code and remove the
syntax errors.

b. Execute the procedure using host variables for the two OUT parameters, one
for the salary and the other for the job ID. Display the salary and job ID for
employee ID 120.

c. Invoke the procedure again, passing an EMPLOYEE_ID of 300. What happens


and why?
Challenge Questions:

1. Write a procedure called raise_salary that uses a parameter with the IN


mode to update a particular employees detail in a copy of the employees
file with a salary that is 10% higher. [6]

a. Use the EXECUTE command to run this procedure

b. Use an anonymous block to run this procedure

2. Write out the following PL/SQL Procedure and observe the IN and OUT
parameters:

CREATE OR REPLACE PROCEDURE query_emp


(id IN employees.employee_id%TYPE,
name OUT employees.last_name%TYPE,
salary OUT employees.salary%TYPE) IS
BEGIN
SELECT last_name, salary INTO name, salary
FROM employees
WHERE employee_id = id;
END query_emp;

(a) Write an anonymous block that runs this procedure with variables to
take the parameters from the procedure created. [4]

(b) What is the significance of the supplied value in the anonymous


block? [2]

(c) What values will the emp_name and emp_sal parameters hold? [2]

(d) Use bind variables to write the execution of query_emp. [2]

3. Write a procedure using the IN OUT mode parameter, phone_no that


formats a 10-digit telephone number with the format (999)999-9999 (e.g. a
number 8006440555 would appear as (800)644-0555. [4]

March 28, 2017

You might also like