Wednesday, 14 September 2011

Can a dynamic cursor be BULK COLLECTed into variables

Question:

Can a dynamic cursor be BULK COLLECTed into variables?

Answer:

if you are living in the world of Oracle9i and later, I have some very good news for you: not only can a dynamic query be BULK COLLECTed into collections, but you can even transfer all your data into a single collection of records! This approach leads to blissful code along the lines of:
DECLARE
TYPE employee_aat IS TABLE OF employee%ROWTYPE
INDEX BY PLS_INTEGER;
l_employee employee_aat;
l_row PLS_INTEGER;
BEGIN
SELECT *
BULK COLLECT INTO l_employee
FROM employee
ORDER BY last_name
;

-- Now iterate through each row in the collection.
l_row := l_employee.FIRST;
WHILE (l_row IS NOT NULL)
LOOP
-- Now work with the row:
l_employee(l_row)
l_row := l_employee.NEXT (l_row);
END LOOP;
END;

No comments:

Post a Comment