PLSQL 5 2 Practice
PLSQL 5 2 Practice
com/academy
Index by table of records A collection which is based on a composite record type; for
example, on the whole DEPARTMENTS row
Try It / Solve It
1. PL/SQL collections:
4. All the data stored in the employees table about a specific employee.
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
2
C. What is the difference between an INDEX BY table and a database table such as
EMPLOYEES or COUNTRIES?
D. Describe the difference between an INDEX BY table and an INDEX BY table of records.
Index by table está en sola una columna o campos mientras que index by table esta son variable
parciales en memoria
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 countries.population%TYPE
INDEX BY BINARY_INTEGER;
v_pops_tab t_pops;
A. Write and execute an anonymous block that declares and populates an INDEX BY table of
countries in South America (region_id = 5). The table should use country_id as a primary key,
and should store the country names as the element values. The data should be stored in the
table in ascending sequence of country_id. The block should not display any output. Save your
code
.
DECLARE
Type t_count_names is table of countries.country_name%TYPE
Index by binary_integer;
V_count_names t_count_names;
Cursor count_cur is
Select country_id, country_name
From countries
Where region_id = 5
Order by coutry_id;
Count_rec count_cur%ROWTYPE;
BEGIN
Open count_cur;
Loop
Fetch count_cur into count_rec;
Exit when count_cur%NOTFOUND;
V_count_names(count_rec.country_id) := count_rec.country_name;
End loop;
Close count_cur;
End;
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
B. Modify the block so that after populating the INDEX BY table, it uses a FOR loop to display the
contents of the INDEX BY table. You will need to use the FIRST, LAST, and EXISTS table
methods. Execute the block and check the displayed results. Save your code.
Declare
Type t_count_names is table of countries.country_name%TYPE
Index by binary_integer;
V_count_names t_count_names;
Cursor count_cur is
Select country_id, country_name
From countries
Where region_id = 5
Order by coutry_id;
Count_rec count_cur%ROWTYPE;
BEGIN
Open count_cur;
Loop
Fetch count_cur into count_rec;
Exit when count_cur%NOTFOUND;
V_count_names(count_rec.country_id) := count_rec.country_name;
End loop;
Close count_cur;
For I in v_count_names.FIRST .. v_count_names.LAST
Loop
If
V_count_names.EXISTS(i) then DBMS_OUTPUT.PUT_LINE(I || 89 || v_count_names(i));
End if;
End loop;
End;
C. Modify the block again so that instead of displaying all the contents of the table, it displays only
the first and last elements and the number of elements in the INDEX BY table. Execute the
block and check the displayed results.
DECLARE
Type t_count_names is table of countries.country_name%TYPE
Index by binary_integer;
V_count_names t_count_names;
Cursor count_cur is
Select country_id, country_name
From countries
Where region_id = 5
Order by coutry_id;
Count_rec count_cur%ROWTYPE;
BEGIN
Open count_cur;
Loop
Fetch count_cur into count_rec;
Exit when count_cur%NOTFOUND;
V_count_names(count_rec.country_id) := count_rec.country_name;
End loop;
Close count_cur;
DBMS_OUTPUT.PUT_LINE(v_count_names.FIRST ||9 8||
v_count_names(v_count_names.FIRST));
DBMS_OUTPUT.PUT_LINE(v_count_names.LAST ||9 8||
v_count_names(v_count_names.LAST));
DBMS_OUTPUT.PUT_LINE(8 Amount of countries: 8 ||
v_count_names(v_count_names.COUNT));
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 employee’s last name, job id, and salary.
The data should be stored in the INDEX BY table of records in ascending sequence of
employee id. The block should not display any output.
Hint: declare a cursor to fetch the employee data, then declare the INDEX BY table as cursor
name%ROWTYPE. Save your code.
DECLARE
Cursor emp_cur is
Select employee_id, last_name, salary, job_id
From employees
Order by employee_id;
Emp_rec emp_cur%ROWTYPE
Index by binary_integer;
V_emp_data t_emp_data;
BEGIN
Open emp_cur;
Loop
Fetch emp_cur into emp_rec;
Exit when emp_cur%NOTFOUND;
V_emp_data(emp.rec.employee_id) := emp_rec;
End loop;
Close emp_cur;
END;
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.
Declare
Cursor emp_cur is
Select employee_id, last_name, salary, job_id
From employees
Order by employee_id;
Emp_rec emp_cur%ROWTYPE
Index by binary_integer;
V_emp_data t_emp_data;
BEGIN
Open emp_cur;
Loop
Fetch emp_cur into emp_rec;
Exit when emp_cur%NOTFOUND;
V_emp_data(emp.rec.employee_id) := emp_rec;
End loop;
Close emp_cur;
For I in v_emp_data.FIRST .. v_emp_data.LAST
Loop
If V_emp_data.EXISTS(i) then DBMS_OUTPUT.PUT_LINE(v_emp_data(i).employee_id(i) ||
8 9 ||
v_emp_data(i).last_name|| 8 8 ||v_emp_data(i).job_id || 8 9 || v_emp_data(i)salary);
End if;
End loop;
END
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.