首页 > 解决方案 > Oracle 11g - 如何使用表连接从函数返回记录

问题描述

在 Oracle 11g 中,我试图从与表连接的函数调用中返回多列。此函数将employee_id 作为输入,并应将first_name 和last_name 作为employees 表中的两个单独列返回。

我创建了一个类型

create or replace
type mytype as object
  ( val_1 varchar2(100),
    val_2 number
  );
/

和功能

create or replace
function myfunc(p_in number) return mytype is
    v_deptname varchar2(100);
    v_mgrid number;
begin
    select department_name,manager_id into v_deptname,v_mgrid from DEPARTMENTS  where department_id = p_in;
return
 mytype(v_deptname,v_mgrid);
end;
/

两者都成功创建。但是当我执行函数时,

select employee_id, salary, myfunc(department_id) from EMPLOYEES where employee_id in(100,101); 

它给出如下结果,

EMPLOYEE_ID     SALARY
----------- ----------
MYFUNC(DEPARTMENT_ID)(VAL_1, VAL_2)
--------------------------------------------------------------------------------
        100      24000
MYTYPE('Executive', 100)

        101      17000
MYTYPE('Executive', 100)

但我希望我的结果是这样的,

EMPLOYEE_ID     SALARY VAL_1                               VAL_2
----------- ---------- ------------------------------ ----------
        100      24000 Executive                             100
        101      17000 Executive                             100

请帮助实现这一目标。谢谢

标签: oracleplsqloracle11g

解决方案


好吧,您可能还缺少另一种类型,基于MYTYPE.

这是一个示例(我正在使用 Scott 的模式,因为我没有您的表)。我已经添加DEPTNOMYTYPE,以便能够将结果(由函数返回)与EMP表连接起来。

这就是你所拥有的:

SQL> create or replace type mytype as object
  2    (deptno number,
  3     dname  varchar2(20),
  4     loc    varchar2(20));
  5  /

Type created.

这就是你所缺少的:

SQL> create or replace type mytab as table of mytype;
  2  /

Type created.

一个函数:注意第9行:

SQL> create or replace function myfunc (p_in number) return mytab is
  2    v_dname varchar2(20);
  3    v_loc   varchar2(20);
  4  begin
  5    select dname, loc
  6      into v_dname, v_loc
  7      from dept
  8      where deptno = p_in;
  9    return mytab(mytype(p_in, v_dname, v_loc));
 10  end myfunc;
 11  /

Function created.

测试:

SQL> select * from table(myfunc(10));

    DEPTNO DNAME                LOC
---------- -------------------- --------------------
        10 ACCOUNTING           NEW YORK

SQL>
SQL> select e.ename, e.sal, m.dname, m.loc
  2  from emp e join table(myfunc(e.deptno)) m on m.deptno = e.deptno
  3  where e.deptno = 10
  4  order by m.dname, e.ename;

ENAME             SAL DNAME                LOC
---------- ---------- -------------------- --------------------
CLARK            2450 ACCOUNTING           NEW YORK
KING            10000 ACCOUNTING           NEW YORK
MILLER           1300 ACCOUNTING           NEW YORK

SQL>

推荐阅读