从 PL-SQL 函数返回 2 个值

发布于 2024-09-14 19:28:09 字数 28 浏览 4 评论 0原文

如何从 PL-SQL 函数返回 2 个值?

How can i return 2 values from a PL-SQL function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

全部不再 2024-09-21 19:28:09

我不提倡为第二个值创建一个带有 OUT 参数的函数,因为我喜欢将函数视为一个纯粹的概念:函数对一个或多个输入执行操作以产生一个输出。它不应该改变它的任何参数或有任何其他“副作用”。

因此,如果您需要两个输出,请编写一个过程

procedure get_sqrt_and_half
   ( p_input number
   , p_sqrt OUT number
   , p_half OUT number
   )
is
begin
   p_sqrt := sqrt(p_input);
   p_half := p_input/2;
end;

I would not advocate creating a function with an OUT parameter for the second value, because I like to think of functions as a pure concept: a function performs an operation on one or more inputs to produce one output. It shouldn't change any of its arguments or have any other "side effects".

So if you need two outputs, write a procedure instead:

procedure get_sqrt_and_half
   ( p_input number
   , p_sqrt OUT number
   , p_half OUT number
   )
is
begin
   p_sqrt := sqrt(p_input);
   p_half := p_input/2;
end;
追我者格杀勿论 2024-09-21 19:28:09

函数只能返回单个 SQL 类型,但可以是具有多个值的用户定义类型。在推荐此解决方案之前,我需要更多地了解实际的最终要求,但这是有可能的。

create or replace type a_b is object (a number, b number);
/

create or replace function ret_a_b return a_b is
begin
  return a_b(1,2);
end;
/

select ret_a_b from dual;

select d.rab.a, d.rab.b from (select ret_a_b rab from dual) d;

A function can only return a single SQL type, but that can be a user-defined type with multiple values. I'd need to know more about the actual end requirements before I'd recommend this as a solution, but it is a possibility.

create or replace type a_b is object (a number, b number);
/

create or replace function ret_a_b return a_b is
begin
  return a_b(1,2);
end;
/

select ret_a_b from dual;

select d.rab.a, d.rab.b from (select ret_a_b rab from dual) d;
纵情客 2024-09-21 19:28:09

您可以直接返回一个值,也可以将另一个值作为 OUT 参数返回。或者您返回包含这两个值的记录。在大多数情况下,第一个选项更容易实现。

You can return one value directly and another one as an OUT parameter. Or you return a record that contains both values. The first option is, in most cases, simpler to do.

半边脸i 2024-09-21 19:28:09
**If you are wanting to use it in SQL, then you would need a pipelined function e.g.**

CREATE OR REPLACE TYPE myemp AS OBJECT
 ( empno    number,
   ename    varchar2(10),
   job      varchar2(10),
   mgr      number,
   hiredate date,
   sal      number,
   comm     number,
   deptno   number
 );

 CREATE OR REPLACE TYPE myrectable AS TABLE OF myemp ;


    enter code here

CREATE OR REPLACE FUNCTION pipedata(p_min_row number, p_max_row number) RETURN myrectable PIPELINED IS
    v_obj myemp := myemp(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
 BEGIN
   FOR e IN (select *
             from (
                   select e.*
                         ,rownum rn
                   from (select * from emp order by empno) e
                  )
             where rn between p_min_row and p_max_row)
   LOOP
     v_obj.empno    := e.empno;
     v_obj.ename    := e.ename;
     v_obj.job      := e.job;
     v_obj.mgr      := e.mgr;
     v_obj.hiredate := e.hiredate;
     v_obj.sal      := e.sal;
     v_obj.comm     := e.comm;
     v_obj.deptno   := e.deptno;
     PIPE ROW (v_obj);
   END LOOP;
   RETURN;
 END;

SQL> select * from table(pipedata(1,5));
**If you are wanting to use it in SQL, then you would need a pipelined function e.g.**

CREATE OR REPLACE TYPE myemp AS OBJECT
 ( empno    number,
   ename    varchar2(10),
   job      varchar2(10),
   mgr      number,
   hiredate date,
   sal      number,
   comm     number,
   deptno   number
 );

 CREATE OR REPLACE TYPE myrectable AS TABLE OF myemp ;


    enter code here

CREATE OR REPLACE FUNCTION pipedata(p_min_row number, p_max_row number) RETURN myrectable PIPELINED IS
    v_obj myemp := myemp(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
 BEGIN
   FOR e IN (select *
             from (
                   select e.*
                         ,rownum rn
                   from (select * from emp order by empno) e
                  )
             where rn between p_min_row and p_max_row)
   LOOP
     v_obj.empno    := e.empno;
     v_obj.ename    := e.ename;
     v_obj.job      := e.job;
     v_obj.mgr      := e.mgr;
     v_obj.hiredate := e.hiredate;
     v_obj.sal      := e.sal;
     v_obj.comm     := e.comm;
     v_obj.deptno   := e.deptno;
     PIPE ROW (v_obj);
   END LOOP;
   RETURN;
 END;

SQL> select * from table(pipedata(1,5));
想你只要分分秒秒 2024-09-21 19:28:09

尝试使用 OUT 参数:

create or replace function f(a IN NUMBER, b OUT NUMBER) RETURN NUMBER IS
BEGIN
  b := a;
  RETURN a;
END f;

Try using OUT parameters:

create or replace function f(a IN NUMBER, b OUT NUMBER) RETURN NUMBER IS
BEGIN
  b := a;
  RETURN a;
END f;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文