首页 > 解决方案 > Oracle批量收集成三列

问题描述

如何在下面的函数中添加多个例如。l_col2l_col3

l_col2 =  Select sal as val2 from emp where empno = P_ID

l_col3 =  Select deptno as val3 from emp where empno = P_ID

create or replace function F_MY_FUNC
    ( 
         P_ID IN number
       )
          return  sys.odcinumberlist
        as
          l_coll  sys.odcinumberlist;
        begin
          select * bulk collect into l_coll
          from (  
                 Select ename as val from emp where empno = P_ID
                 union all
                 Select ename as val from myemp where empno = P_ID
              );
    
         return l_coll;
       end;

标签: sqloracleplsqlbulkinsert

解决方案


你可以,但不能进入SYS.ODCINUMBERLIST- 创建你自己的类型。

SQL> create or replace type t_row as object
  2    (empno number, ename varchar2(10), job varchar2(10));
  3  /

Type created.

SQL> create or replace type t_tab as
  2    table of t_row;
  3  /

Type created.

SQL> declare
  2    l_tab t_tab;
  3  begin
  4    select t_row(empno, ename, job)      --> 3 columns 
  5      bulk collect into l_tab            --> into one collection
  6      from emp
  7      where deptno = 20;
  8  end;
  9  /

PL/SQL procedure successfully completed.

SQL>

推荐阅读