Bulk-Fetching from a Cursor Into One or More Collections
DECLARE
TYPE namelist IS TABLE OF employees.last_name%TYPE;
TYPE sallist IS TABLE OF employees.salary%TYPE;
CURSOR c1 IS
SELECT last_name,
salary
FROM employees
WHERE salary > 10000;
names NAMELIST;
sals SALLIST;
TYPE reclist IS TABLE OF c1%ROWTYPE;
recs RECLIST;
v_limit PLS_INTEGER := 10;
PROCEDURE Print_results
IS
BEGIN
IF names IS NULL
OR names.COUNT = 0 THEN -- check if collections are empty
dbms_output.Put_line('No results!');
ELSE
dbms_output.Put_line('Results: ');
FOR i IN names.FIRST.. names.LAST LOOP
dbms_output.Put_line(' Employee '
||Names(i)
||': $'
||Sals(i));
END LOOP;
END IF;
END;
BEGIN
dbms_output.Put_line('--- Processing all results at once ---');
OPEN c1;
FETCH c1 BULK COLLECT INTO names,sals;
CLOSE c1;
Print_results();
dbms_output.Put_line('--- Processing '
||v_limit
||' rows at a time ---');
OPEN c1;
LOOP
FETCH c1 BULK COLLECT INTO names,sals LIMIT v_limit;
EXIT WHEN names.COUNT = 0;
Print_results();
END LOOP;
CLOSE c1;
dbms_output.Put_line('--- Fetching records rather than columns ---');
OPEN c1;
FETCH c1 BULK COLLECT INTO recs;
FOR i IN recs.FIRST.. recs.LAST LOOP
-- Now all the columns from the result set come from a single record
dbms_output.Put_line(' Employee '
||Recs(i).last_name
||': $'
||Recs(i).salary);
END LOOP;
END;
/
OUTPUT:-
--- Processing all results at once ---
Results:
Employee Hartstein: $13000
Employee Higgins: $12000
Employee King: $24000
Employee Kochhar: $17000
Employee De Haan: $17000
Employee Greenberg: $12000
Employee Raphaely: $11000
Employee Russell: $14000
Employee Partners: $13500
Employee Errazuriz: $12000
Employee Cambrault: $11000
Employee Zlotkey: $10500
Employee Vishney: $10500
Employee Ozer: $11500
Employee Abel: $11000
--- Processing 10 rows at a time ---
Results:
Employee Hartstein: $13000
Employee Higgins: $12000
Employee King: $24000
Employee Kochhar: $17000
Employee De Haan: $17000
Employee Greenberg: $12000
Employee Raphaely: $11000
Employee Russell: $14000
Employee Partners: $13500
Employee Errazuriz: $12000
Results:
Employee Cambrault: $11000
Employee Zlotkey: $10500
Employee Vishney: $10500
Employee Ozer: $11500
Employee Abel: $11000
--- Fetching records rather than columns ---
Employee Hartstein: $13000
Employee Higgins: $12000
Employee King: $24000
Employee Kochhar: $17000
Employee De Haan: $17000
Employee Greenberg: $12000
Employee Raphaely: $11000
Employee Russell: $14000
Employee Partners: $13500
Employee Errazuriz: $12000
Employee Cambrault: $11000
Employee Zlotkey: $10500
Employee Vishney: $10500
Employee Ozer: $11500
Employee Abel: $11000
PL/SQL procedure successfully completed.
0 comments:
Post a Comment