Stored Procedures with Multiple Result Sets in Oracle PL/SQL
In Oracle PL/SQL, stored procedures can be crafted to return multiple result sets based on different arguments. This can enhance query flexibility and enable efficient data retrieval. To achieve this, the concept of nested tables, also known as collection types, comes into play.
Consider a scenario where you require a stored procedure to dynamically retrieve employee records based on user-supplied criteria. Here's how you can approach it:
Here's an example implementation:
TYPE emp_obj IS OBJECT (empno NUMBER, ename VARCHAR2(10)); TYPE emp_tab IS TABLE OF emp_obj; CREATE OR REPLACE FUNCTION all_emps RETURN emp_tab IS l_emp_tab emp_tab := emp_tab(); n INTEGER := 0; BEGIN FOR r IN (SELECT empno, ename FROM emp) LOOP l_emp_tab.extend; n := n + 1; l_emp_tab(n) := emp_obj(r.empno, r.ename); END LOOP; RETURN l_emp_tab; END;
Now, you can execute this stored procedure in plain SQL:
SELECT * FROM TABLE(all_emps);
This query will return a dynamically generated result set with multiple rows, where each row represents an employee object.
The above is the detailed content of How Can Oracle PL/SQL Stored Procedures Return Multiple Result Sets?. For more information, please follow other related articles on the PHP Chinese website!