The simplest possible approach is to return a cursor. Something like
create or replace function get_emps_in_dept( p_dept_in IN number )
return sys_refcursor
is
l_ret sys_refcursor;
begin
open l_ret
for select *
from emp
where deptno = p_dept_in;
return l_ret;
end;
You can then call that in SQL*Plus by declaring a host variable (most front-end languages will treat a function returning a SYS_REFCURSOR
very much like a SELECT
statement that returns a cursor)
SQL> variable rc refcursor;
SQL> exec :rc := get_emps_in_dept( 30 );
PL/SQL procedure successfully completed.
SQL> print rc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1601 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1251 500
30
7654 MARTIN SALESMAN 7698 28-SEP-81 1251 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2851
30
7844 TURNER SALESMAN 7698 08-SEP-81 1501 0
30
7900 JAMES CLERK 7698 03-DEC-81 951
30
6 rows selected.
You can return a collection, either all at once or pipelined, but that requires more code to create and maintain the objects. I love pipelined table functions if you need to embed PL/SQL logic in the query itself that would be difficult to follow in SQL. But they're overkill if you just need to run a simple query and return the results.
SELECT
(without involving plsql)?