Reputation: 5560
Oracle stored procedure has OUT parameter and returns result set, e.g.
create or replace procedure foo(empId IN NUMBER, maxSalary OUT NUMBER) AS BEGIN
select * from Employee e where e.id >=empId;
select max(salary) into maxSalary from Employee;
END;
ERROR:
PLS-00428: an INTO clause is expected in this SELECT statement
Mysql stored procedure can return both result sets and out parameters. How to do it for oracle db?
Upvotes: 3
Views: 14173
Reputation: 31648
In Oracle, you cannot run direct select statements without an INTO
clause.
If you are using Oracle 12c and above, you may use a REF CURSOR
and DBMS_SQL.RETURN_RESULT
create or replace procedure foo(empId IN NUMBER, maxSalary OUT NUMBER) AS
q SYS_REFCURSOR;
BEGIN
OPEN q FOR select * from Employee e where e.id >=empId;
DBMS_SQL.return_result (q); -- This will display the result
select max(salary) into maxSalary from Employee;
END;
For previous versions ( 11g,10g) , You could pass a REF CURSOR
as an OUT
parameter and print it from sqlplus
or TOAD by running as script.
create or replace procedure foo(empId IN NUMBER, maxSalary OUT NUMBER,
q OUT SYS_REFCURSOR) AS
BEGIN
OPEN q FOR select * from Employee e where e.id >=empId;
select max(salary) into maxSalary from Employee;
END;
Define bind variables before calling the procedure.
VARIABLE v_empID NUMBER
VARIABLE v_maxsalary NUMBER
VARIABLE v_q REFCURSOR
EXEC :v_empID := 101
EXEC foo(:v_empID,:v_maxsalary,:v_q )
PRINT v_q -- This will display the result from the query.
Upvotes: 9