Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: Return multiple values in a function

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;
like image 914
Jamil Smith Avatar asked May 31 '12 21:05

Jamil Smith


1 Answers

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;
like image 152
Guru Avatar answered Sep 22 '22 11:09

Guru