I'm used to Microsoft SQL Server, where the last SELECT query of a stored procedure determines what is returned.

In Oracle, I'm always using a SYS_REFCURSOR OUT parameter to return data from queries.

Are there other options of returning data from SELECT queries in Oracle stored procedures?

How about SELECT queries that select only one row? Is a SYS_REFCURSOR still necessary?

EDIT: I need to know the answer for Oracle 11g R2 (should have mentioned that explicitly instead of just in the tags).

有帮助吗?

解决方案

Until now Oracle has not supported the SQL Server style of procedures implicitly returning a result set, you have had to explicitly return something - which could be a SYS_REFCURSOR or a collection or whatever.

In Oracle 12C a new feature has been added called Implicit Statement Results, which is designed to emulate the SQL Server way of working. However, this is really intended to support migration of existing code from SQL Server; for fresh Oracle development you would be best advised to learn the way Oracle normally does things.

其他提示

Oracle PL/SQL Procedures can return all the supported basic datattype( date,varchar2,number) plus complex ( records, tables, varray)

Another option is the pipelined function, in which you call the function as:

select ...
from   table(my_function(param1 => 1, ...))

See docs for details.

Just for clarifying, ORACLE procedures cannot 'RETURN" per se, a SYS_REFCURSOR OUT parameter is more like changing the value of a variable reference inside the procedure. Apart from SYS_REFCURSOR, if you are returning only one row of a table, say EMPLOYEE, you can also define a record as EMPLOYEE%ROWTYPE and use it as a OUT type.

Or like:

PROCEDURE pr_proc (v_input in number
                   v_emp_row out EMPLOYEE%ROWTYPE )
    IS
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top