Retrieving Query Results With BULK COLLECT
DECLARE
TYPE numtab IS TABLE OF employees.employee_id%TYPE;
TYPE nametab IS TABLE OF employees.last_name%TYPE;
enums NUMTAB; -- No need to initialize the collections.
names NAMETAB; -- Values will be filled in by the SELECT INTO.
PROCEDURE Print_results
IS
BEGIN
IF enums.COUNT = 0 THEN
dbms_output.Put_line('No results!');
ELSE
dbms_output.Put_line('Results:');
FOR i IN enums.FIRST.. enums.LAST LOOP
dbms_output.Put_line(' Employee #'
||Enums(i)
||': '
||Names(i));
END LOOP;
END IF;
END;
BEGIN
-- Retrieve data for employees with Ids greater than 1000
SELECT employee_id,
last_name
BULK COLLECT INTO enums,names
FROM employees
WHERE employee_id > 1000;
-- The data has all been brought into memory by BULK COLLECT
-- No need to FETCH each row from the result set
Print_results();
-- Retrieve approximately 20% of all rows
SELECT employee_id,
last_name
BULK COLLECT INTO enums,names
FROM employees SAMPLE ( 20 );
Print_results();
END;
/
OUTPUT:-
No results!
Results:
Employee #200: Whalen
Employee #206: Gietz
Employee #100: King
Employee #104: Ernst
Employee #107: Lorentz
Employee #126: Mikkilineni
Employee #127: Landry
Employee #130: Atkinson
Employee #135: Gee
Employee #156: King
Employee #158: McEwen
Employee #163: Greene
Employee #168: Ozer
Employee #169: Bloom
Employee #170: Fox
Employee #171: Smith
Employee #173: Kumar
Employee #184: Sarchand
Employee #186: Dellinger
Employee #189: Dilly
Employee #193: Everett
PL/SQL procedure successfully completed.
0 comments:
Post a Comment