5

I'm trying to return a multiple values in a %rowtype from a function using two table(employees and departments), but it not working for me.

create or replace function get_employee
 (loc in number)
return mv_emp%rowtype
as  
   emp_record mv_emp%rowtype;
begin
   select a.first_name, a.last_name, b.department_name into emp_record 
   from employees a, departments b 
   where a.department_id=b.department_id and location_id=loc;

   return(emp_record);  
end;
0

3 Answers 3

10

The above function compiled without any error? What is the type of MV_EMP? Ideally, it should be something like below.

create or replace type emp_type
(
first_name varchar2(20)
, last_name varchar2(20)
, depart_name varchar2(20)
)
/
create or replace function get_employee
 (loc in number)
return emp_type
as  
   emp_record emp_type;
begin
   select a.first_name, a.last_name, b.department_name into emp_record 
   from employees a, departments b 
   where a.department_id=b.department_id and location_id=loc;

   return(emp_record);  
end;
Sign up to request clarification or add additional context in comments.

4 Comments

In my oracle version, 11g, the type declaration does not compile when specified like this. I had to declare the type AS OBJECT
I also had to write this syntax (in 11g): SELECT x.some_field, y.something_else INTO myobject.field1, myobject.field2 FROM ...
I had to initialise the object first also, like this: myobject mytype := mytype('', '');
Thanks @andersand. I did this in 10g and was successful.
1
create type t_row as object (a varchar2(10));

create type t_row_tab as table of t_row;

We will now create a function which will split the input string.

create or replace function get_number(pv_no_list in varchar2) return t_row_tab is
lv_no_list t_row_tab := t_row_tab();
begin

  for i in (SELECT distinct REGEXP_SUBSTR(pv_no_list, '[^,]+', 1, LEVEL) no_list FROM dual
  CONNECT BY REGEXP_SUBSTR(pv_no_list, '[^,]+', 1, LEVEL) IS NOT NULL)
  loop
  lv_no_list.extend;
  lv_no_list(lv_no_list.last) := t_row(i.no_list);
  end loop;

  return lv_no_list;

end get_number;

Once the function is in place we can use the table clause of sql statement to get the desired result. As desired we got multiple values returned from the function.

SQL> select * from table(get_number('1,2,3,4'));


A
----------
1
3
2
4

So now our function is simply behaving like a table. There can be a situation where you want these comma separated values to be a part of "IN" clause.

For example :

select * from dummy_table where dummy_column in ('1,2,3,4');

But the above query will not work as '1,2,3,4' is a string and not individual numbers. To solve this problem you can simply use following query.

select * from dummy_table where dummy_column in ( select * from table(get_number('1,2,3,4')) );

References : http://www.oraclebin.com/2012/12/returning-multiple-values-from-function.html

Comments

-2
CREATE OR replace FUNCTION Funmultiple(deptno_in IN NUMBER)
  RETURN NUMBER AS v_refcursur SYS_REFCURSOR;
  BEGIN
    OPEN v_refcursor FOR
    SELECT *
    FROM   emp
    WHERE  deptno = deptno_in;
    
    retun v_refcursor;
  END;

To call it, use:

variable x number
exec :x := FunMultiple(10);
print x 

2 Comments

This does not even compile, even after correcting the spelling mistakes
please include working code at-least with proper syntax and test it once before posting it on the platforms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.