【问题标题】:Oracle function return tableoracle函数返回表
【发布时间】:2021-03-11 13:55:59
【问题描述】:

我不知道什么是我的问题的最佳解决方案。我需要一个参数的函数,结果是

VAL
-----
1
2
3

在函数中我需要把 union all 来获取所有值。

Select column_1 as VAL from my_table where id = P_FUNCTION_PARAMETER --return 1
union all
Select column_2 as VAL from my_table where id = P_FUNCTION_PARAMETER --return 2
union all
Select column_3 as VAL from my_table where id = P_FUNCTION_PARAMETER; --return 3

什么是最好的解决方案?

【问题讨论】:

  • 所以column_1column_2column_3 都是number 数据类型?而且,为什么你需要用一个函数来做到这一点?在普通 SQL 中获取这样的表是一项微不足道的任务。
  • @mathguy 在我的应用程序查询编辑器中调用 sql 查询时出现错误:值太长了 XXX 个字符!

标签: sql oracle stored-functions


【解决方案1】:

“最佳”取决于。返回一个引用游标或一个集合,无论你喜欢哪个。

例如:

SQL> create or replace function f_test_rc
  2    return sys_refcursor
  3  is
  4    rc sys_refcursor;
  5  begin
  6    open rc for
  7      select 1 from dual union all
  8      select 2 from dual union all
  9      select 3 from dual;
 10
 11    return rc;
 12  end;
 13  /

Function created.

SQL> select f_test_rc from dual;

F_TEST_RC
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

         1
----------
         1
         2
         3

SQL> create or replace function f_test_coll
  2    return  sys.odcinumberlist
  3  as
  4    l_coll  sys.odcinumberlist;
  5  begin
  6    select * bulk collect into l_coll
  7    from (select 1 from dual union all
  8          select 2 from dual union all
  9          select 3 from dual
 10         );
 11
 12    return l_coll;
 13  end;
 14  /

Function created.

SQL> select * from table(f_test_coll);

COLUMN_VALUE
------------
           1
           2
           3

SQL>

【讨论】:

  • 您注意到 OP 问题的第二部分了吗?他不需要输出中的硬编码数字,他需要从表中提取的值。
  • @Littlefoot Tnx 寻求帮助,您的第一个示例在我的示例中返回结果是这样的。 postimg.cc/F7NFYtSt,第二个例子我没有 sys.odcinumberlist
【解决方案2】:

首先让我们建立一个小表进行测试:

create table my_table
( id       number primary key
, column_1 number
, column_2 number
, column_3 number
);

insert into my_table
  select 1008, 3,   -8,  0.2  from dual union all
  select 1002, 6, null, -1.2 from dual
;
commit;

函数可能如下所示。请注意,我不使用union all - 这将需要阅读表格三遍,而只有一次就足够了。

create or replace function my_function (p_function_parameter number)
  return sys.odcinumberlist
as
  arr sys.odcinumberlist;
begin
  select case ord when 1 then column_1
                  when 2 then column_2
                  when 3 then column_3 end
  bulk   collect into arr
  from   my_table cross join
         (select level as ord from dual connect by level <= 3)
  where  id = p_function_parameter
  order  by ord
  ;
  
  return arr;
end;
/

函数可以这样使用,例如:(在旧版本中,您可能需要将函数调用包装在 table 运算符中)

select * from my_function(1002);

COLUMN_VALUE
------------
           6

        -1.2

【讨论】:

  • Tnx 回答,我可能没有 sys.odcinumberlist 吗?
  • @user_odoo - 这太奇怪了;它是 Oracle 本身提供的 varray 数据类型,它不是您创建的。我完全使用它,因此我不需要定义单独的数据类型。
  • @user_odoo 在任何情况下:您可以create or replace type my_numberlist as varray(100) of number 然后在函数中使用它而不是sys.odcinumberlist。但是,如果您的系统上无法使用 sys.odcinumberlist,请询问您的 DBA,因为除非有人在某处做错了什么,否则这种情况不应该发生。
猜你喜欢
  • 2012-09-02
  • 2023-03-27
  • 2016-09-02
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
相关资源
最近更新 更多