DECLARE
CURSOR cursor1 IS SELECT * FROM emp;
emp_rec cursor1%ROWTYPE;
TYPE emp_array IS VARRAY(20) OF cursor1%ROWTYPE;
emp_arr emp_array;
no NUMBER;
i NUMBER := 1;
BEGIN
emp_arr := emp_array(); -- initialize the array
OPEN cursor1;
LOOP
FETCH cursor1 INTO emp_rec;
EXIT WHEN cursor1%NOTFOUND;
emp_arr.EXTEND;
emp_arr(i) := emp_rec;
i := i + 1;
END LOOP;
CLOSE cursor1;
FOR j IN emp_arr.FIRST..emp_arr.LAST
LOOP
DBMS_OUTPUT.PUT_LINE (emp_arr(j).ename);
END LOOP;
END;
Output:
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKEP
--------------------------------------------------------
DECLARE
TYPE emp_nt IS TABLE OF VARCHAR2(15);
enames_nt emp_nt := ename_nt(‘David', 'James', ‘Scott'); --
Initializing the nested table element.
BEGIN
FOR i IN names.FIRST .. names.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(names(i)); -- reference to nested table
elements
END LOOP;
END;
------------------------------------------------------
DECLARE
CURSOR cursor1 IS SELECT * FROM emp;
emp_rec cursor1%ROWTYPE;
TYPE emp_NT IS TABLE OF cursor1%ROWTYPE;
emp_n emp_NT;
no NUMBER;
i NUMBER := 1;
BEGIN
emp_n := emp_NT();
OPEN cursor1;
LOOP
FETCH cursor1 INTO emp_rec;
EXIT WHEN cursor1%NOTFOUND;
emp_n.EXTEND;
emp_n.(i) := emp_rec;
i := i + 1;
END LOOP;
CLOSE cursor1;
FOR j IN emp_n.FIRST..emp_n.LAST
LOOP
DBMS_OUTPUT.PUT_LINE (emp_n(j).ename);
END LOOP;
END;
Output:
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE