1
votes

In Oracle 11g , I am trying to return multiple columns from a function call which is joined with a tables. This function takes employee_id as input and should return first_name and last_name as two separate columns from employees table.

I created a type

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

And function

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;
/

Both got created successfully. But when I execute function ,

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

It gives result like below,

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

        101      17000
MYTYPE('Executive', 100)

But i want my result to be like ,

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

Please help to achieve this. Thanks

2
You did some unnecessary use of variables. You must simplfy your code as shown in my demo below. Also incase of many employees in a deptno you code tends to fail since you are using scalar variables.XING

2 Answers

1
votes

Well, you might be missing yet another type, based on MYTYPE.

Here's an example (I'm using Scott's schema as I don't have your tables). I've added DEPTNO into MYTYPE so that I'd be able to join the result (returned by the function) with the EMP table.

This is what you have:

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

Type created.

This is what you are missing:

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

Type created.

A function: note line 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.

Testing:

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>
0
votes

You can try as below:

--Tables

CREATE TABLE dept (
    deptno   NUMBER,
    dname    VARCHAR2 (20),
    loc      VARCHAR2 (20)
);
/  

CREATE TABLE employee (
    employee_id   NUMBER,
    salary        NUMBER
);

Code:

CREATE OR REPLACE TYPE mytype AS OBJECT (
    deptno   NUMBER,
    dname    VARCHAR2 (20),
    loc      VARCHAR2 (20)
);

CREATE OR REPLACE TYPE mytab AS TABLE OF mytype;

CREATE OR REPLACE FUNCTION myfunc ( p_in NUMBER) 
RETURN mytab 
IS
    v_var   mytab := mytab ();
BEGIN
    SELECT
        mytype (
            deptno,
            dname,
            loc
        )
    BULK COLLECT INTO
        v_var
    FROM
        dept
    WHERE
        deptno = p_in;
    return (v_var);
END myfunc;
/

Execution:

SELECT
    e.*,
    t.deptno,
    t.dname   
FROM
    employee e
    CROSS JOIN 
     TABLE ( myfunc (100) ) t ;

Output:

Employee_Id  SALARY DEPT DNAME      
---------    ------ ---- ------    
1001         20000  100  Executive  
1002         25000  100  Executive