Вы находитесь на странице: 1из 2

Lab Assignment #6

CIS 208A PL/SQL Programming and SQL


1
Copy and execute the following anonymous block. Then modify it to declare and use a
single record instead of a scalar variable for each column. Make sure that your code will still
work if an extra column is added to the departments table later. Execute your modified block
and save your code.
DECLARE
v_dept_
id departments.department_id%TYPE;
v_dept_name departments.department_name%TYPE;
v_mgr_id departments.manager_id%TYPE;
v_loc_id departments.location_id%TYPE;
BEGIN
SELECT department_id, department_name,
manager_id, location_id
INTO v_dept_id, v_dept_name,
v_mgr_id, v_loc_id
FROM departments
WHERE department_id = 80;
DBMS_OUTPUT.PUT_LINE(v_dept_id || ' ' || v_dept_name
|| ' ' || v_mgr_id
|| ' ' || v_loc_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('This department does not exist');
END;
2

PL/SQL collections:
A. In your own words, describe what a PL/SQL collection is.
B. Which of the following are collections and which are not?
1.
2.
3.
4.

A list of all employees last names


The character value Chang
The populations of all countries in Europe
All the data stored in the employees table about a specific employee.

C. That is the difference between an INDEX BY table and a database table such as
employees or wf_countries?
D. .Describe the difference between an INDEX BY table and an INDEX BY table of
records.
Lab_6_Fa13.doc

E. Look at the following code. Describe the difference between t_pops and
v_pops_tab. Is v_pops_tab an INDEX BY table or an INDEX BY table of records? How do
you know?
DECLARE
TYPE t_pops IS TABLE OF wf_countries.population%TYPE
INDEX BY BINARY_INTEGER;
v_pops_tab t_pops;
3. Table of Records:
A. Write and execute an anonymous block that declares and populates an INDEX
by table of records containing employee data. The table of records should use the
employee id as a primary key, and each element should contain an employees last
name, job id and salary. The data should be stored in the table of records in
ascending sequence of employee id. The block should not display any output. Hint:
declare acursor to fetch the employee data, then declare the INDEX BY table as
cursor-name%ROWTYPE Save your code.
B.
Modify the block so that after populating the table of records, it uses a FOR loop
to display to display the contents. You will need to use the FIRST, LAST and EXISTS
table methods. Execute the block and check the displayed results. Save your code.

C. Convert the anonymous block from step 3A to a package named tab_rec_pkg


that declares both the cursor and the type as global variables in the package
specification. The block code should be converted to a public packaged procedure
named pop_emp_tab, which does not display anything, but declares the INDEX BY
table of records as an OUT parameter. Execute your code to create the package
specification and body.

D. Write and execute an anonymous block that calls the pop_emp_tab packaged
procedure and displays the returned table of records of employee data using a
FOR loop. Save your code.

Lab_6_Fa13.doc

Вам также может понравиться