how to add all those cursor values to the nested table variable and
print them?
Using LOOP you can assign values one by one as below:
Using Nested Table
DECLARE
TYPE my_nested_table IS TABLE OF employee%rowtype;
cursor cur is
select *
from employee;
var_nt my_nested_table := my_nested_table ();
BEGIN
for i in cur
loop
var_nt.extend;
var_nt(var_nt.count) := i; -- Assignment
--Printing. You can add the columns you want
dbms_output.put_line(i.empno ||'-'|| i.ename||'-'||i.sal||'-'|| i.deptno);
end loop;
END;
/
Using Associative Array: - Which could save you from initializing and extending
DECLARE
TYPE my_nested_table IS TABLE OF employee%rowtype index by pls_integer;
cursor cur is
select *
from employee;
var_nt my_nested_table; -- No need to initialize
BEGIN
for i in cur loop
--var_nt.extend; -- No need to extend
var_nt(var_nt.count) := i;
dbms_output.put_line(i.empno ||'-'|| i.ename||'-'||i.sal||'-'|| i.deptno);
end loop;
END;
/
Secondly You can avoid the loop and fetch the values directly in Nested table as shown below:
DECLARE
TYPE my_nested_table IS TABLE OF employee%rowtype;
var_nt my_nested_table := my_nested_table ();
BEGIN
select *
BULK COLLECT INTO var_nt
from employee;
for i in 1..var_nt.count Loop
dbms_output.put_line('Employee No. - '||var_nt(i).empno||' '||'Employee Name - '||var_nt(i).ename);
end loop;
END;
/