Oracle
 sql >> Database >  >> RDS >> Oracle

Oracle 11g - come restituire il record dalla funzione con il join della tabella

Bene, potresti mancare ancora un altro tipo, basato su MYTYPE .

Ecco un esempio (sto usando lo schema di Scott perché non ho le tue tabelle). Ho aggiunto DEPTNO in MYTYPE in modo da poter unire il risultato (restituito dalla funzione) con EMP tabella.

Questo è quello che hai:

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

Type created.

Questo è quello che ti manca:

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

Type created.

Una funzione:nota riga 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.

Test:

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>