gpt4 book ai didi

Oracle存储过程: return both result set and out parameters

转载 作者:行者123 更新时间:2023-12-04 02:02:55 25 4
gpt4 key购买 nike

Oracle存储过程有OUT参数并返回结果集,例如

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;

错误:

PLS-00428: an INTO clause is expected in this SELECT statement

Mysql存储过程既可以返回结果集,也可以返回out参数。 oracle db怎么做?

最佳答案

在 Oracle 中,您不能在没有 INTO 子句的情况下运行直接选择语句。

如果您使用的是 Oracle 12c 及更高版本,您可以使用 REF CURSORDBMS_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;

对于以前的版本(11g,10g),您可以将 REF CURSOR 作为 OUT 参数并从 sqlplus 或 TOAD 打印它通过作为脚本运行。

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;

在调用过程之前定义绑定(bind)变量。

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.

关于Oracle存储过程: return both result set and out parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48838582/

25 4 0