You are on page 1of 5

Selecting Multiple Rows: BULK COLLECT Clause If you need to bring a large quantity of data into local PL/SQL

variables, rather than looping through a result set one row at a time, you can use the BULK COLLECT clause. When you query only certain columns, you can store all the results for each column in a separate collection variable. When you query all the columns of a table, you can store the entire result set in a collection of records, which makes it convenient to loop through the results and refer to different columns.

Limiting the Rows for a Bulk FETCH Operation with the LIMIT Clause
The optional LIMIT clause, allowed only in bulk FETCH statements, limits the number of rows fetched from the database. In Example 1114, with each iteration of the loop, the FETCH statement fetches ten rows (or less) into index-by table empids. The previous values are overwritten. Note the use of empids.COUNT to determine when to exit the loop.

bulk collect with Limiting rows by pls_integer: DECLARE TYPE numtab IS TABLE OF NUMBER INDEX BY PLS_INTEGER; CURSOR c1 IS SELECT empno FROM emp WHERE deptno=30; empids numtab; rows PLS_INTEGER := 3; BEGIN OPEN c1; LOOP

FETCH c1 BULK COLLECT INTO empids LIMIT rows; EXIT WHEN empids.COUNT = 0; DBMS_OUTPUT.PUT_LINE('------- Results from Each Bulk Fetch --------'); FOR i IN 1..empids.COUNT LOOP DBMS_OUTPUT.PUT_LINE( 'Employee Id: ' || empids(i)); END LOOP; END LOOP; CLOSE c1; END;

bulk collect with returning clause:


DECLARE TYPE NAME IS TABLE OF E1.Empno%TYPE; TYPE PAYS IS TABLE OF E1.ename%TYPE; N NAME; P PAYS; BEGIN delete from e1 where deptno=20 returning empno,ename bulk collect into n,p; dbms_output.put_line('deleted'|| SQL%ROWCOUNT|| 'rows:'); for i in n.first..n.last loop dbms_output.put_line('employee #'|| ||n(i) ||': ' || p(i));

end loop; end;

Example 635 Using a Cursor Expression:

DECLARE TYPE emp_cur_typ IS REF CURSOR; emp_cur emp_cur_typ; dept_name departments.department_name%TYPE; emp_name employees.last_name%TYPE; CURSOR c1 IS SELECT department_name, -- second item in the result set is another result set, -- which is represented as a ref cursor and labelled "employees". CURSOR ( SELECT e.last_name FROM employees e WHERE e.department_id = d.department_id) employees FROM departments d WHERE department_name like 'A%'; BEGIN OPEN c1; LOOP FETCH c1 INTO dept_name, emp_cur; EXIT WHEN c1%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Department: ' || dept_name); -- for each row in the result set, the result set from a subquery is processed

-- the set could be passed to a procedure for processing rather than the loop LOOP FETCH emp_cur INTO emp_name; EXIT WHEN emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE('-- Employee: ' || emp_name); END LOOP; END LOOP; CLOSE c1; END;

--Example

633 Fetching from a Cursor --Variable into Collections:

DECLARE TYPE empcurtyp IS REF CURSOR; TYPE namelist IS TABLE OF emp.Ename%TYPE; TYPE sallist IS TABLE OF emP.sal%TYPE; emp_cv empcurtyp; names namelist; sals sallist; BEGIN OPEN emp_cv FOR SELECT ename, sal FROM emp WHERE job='SALESMAN'; FETCH emp_cv BULK COLLECT INTO names, sals; CLOSE emp_cv; -- loop through the names and --sals collections

FOR i IN names.FIRST .. names.LAST LOOP DBMS_OUTPUT.PUT_LINE('Name=' || names(i) || ',salary=' || sals(i)); END LOOP; END;

DIFFERENCES OF VARRAYS AND NESTED TABLES: VARRAYS HAVE MAXIMUM SIZE, AS SPECIFIED AT TIME OF CREATION. WHILE NESTED TABLES DO NOT. VARRAYS ARE STORED IN LINE WITH THE CONTAINING TABLE, WHILE NESTED TABLES ARE STORED IN A SEPARATE TABLE, WHICH CAN HAVE DIFFERENT STORAGE CHARACTERSTICS. ROWS IN THE NESTED TABLE CAN BE ACCESSED INDIVIDUALLY BY USING THE THE CLAUSE, WHERE AS IN THE VARRAYS ALL THE ROWS SHOULD BE ACCESSED AS ONE OBJECT AND IS TO NE MANIPULATED BY USING PL/SQL BLOCKS.

ADVANTAGES: IMPLEMENTATIONS OF ONE-TO-MANY RELATIONSHIPS. Nested Table Vs Varray Stored with in the table

Stored outside the table DML is allowed Stores unlimited data

DML Not Allowed Stores limited data

You might also like