Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Pipelined Table Function - want to return custom values too...

SmithJohn45Aug 15 2023 — edited Aug 15 2023

i have following code in my practice / examples, what if i want to add department_name column to return from this pipelined table function which is not part of employees table, please help how to achieve this?

-- step # 1 object type to match employees record
create or replace type hr.emp_scalar_type as object
 (EMPNO NUMBER(4) ,
  ENAME VARCHAR2(30),
  JOB VARCHAR2(10),
  MGR NUMBER(6),
  HIREDATE DATE,
  SAL NUMBER(8, 2),
  COMM NUMBER(4, 2),
  DEPTNO NUMBER(4)
 )
/
-- step # 2 table type of employees records
create or replace type hr.emp_table_type as table of hr.emp_scalar_type
/
-- step # 3 pipelined function
create or replace function hr.get_emp( p_deptno in number )
 return emp_table_type
 PIPELINED
 as
   TYPE EmpCurTyp IS REF CURSOR RETURN hr.employees%ROWTYPE;
   emp_cv EmpCurTyp;
   l_rec  hr.employees%rowtype;
 begin
   open emp_cv for select * from hr.employees where department_id = p_deptno;
   loop
     fetch emp_cv into l_rec;
     exit when (emp_cv%notfound);
     pipe row( emp_scalar_type( l_rec.employee_id, LOWER(l_rec.last_name),
         l_rec.job_id, l_rec.manager_id, l_rec.hire_date, l_rec.salary, 
         l_rec.commission_pct, l_rec.department_id ) );
   end loop;
   return;
 end;
/


-- step # 4 execute query  
select * from table(hr.get_emp(20));
     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      201 hartstein  MK_MAN           100 17-FEB-04      13000                    20
      202 fay        MK_REP           201 17-AUG-05       6000                    20

regards

This post has been answered by cormaco on Aug 15 2023
Jump to Answer

Comments

Post Details

Added on Aug 15 2023
8 comments
132 views