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!

19c: Pipelined or without Pipelined?

SmithJohn45Aug 19 2024 — edited Aug 19 2024

i was trying to search about Pipelined Table Functions, found some pages where people mentioned that they are not using Pipelined function but another approach without it.

i have created both examples and want to know about seniors opinion which approach is more better and recommended. please guide us. i tested both on livesql.oracle.com both working fine.

-- not a PIPELINED Table Function but returns record(s) without pipelined
create type TXR_RECORD IS OBJECT
(deptno VARCHAR2(5),
 empno  VARCHAR2(10),
 ename  VARCHAR2(100),
 sal    VARCHAR2(20));
/
CREATE type txr_table IS TABLE OF txr_record;
/
-- 2. create function
CREATE OR REPLACE FUNCTION pipe_results (
 p_source VARCHAR2,
 p_from INTEGER,
 p_to INTEGER)
RETURN txr_table
IS
rettab txr_table := txr_table();
BEGIN
SELECT txr_record(empno, deptno, ename, sal)
BULK COLLECT INTO rettab
FROM (SELECT rownum rn, empno, deptno, ename, sal
     FROM scott.emp
     WHERE deptno = p_source
     ORDER BY 1)
WHERE rn <= p_to;
RETURN rettab;
END;
/
-- 3. called from sql
select deptno, empno, ename, sal
 from table(pipe_results(30, 1, 4));
-- ========================================================
-- Pipelined Table Function approach
-- :[ i knew we can create a VIEW but its simple enough just to Understand ]:--
-- :[ developer can do much more to extend it and return rows with more data ]:--
-- 
-- create Object Type 
CREATE OR REPLACE TYPE hr_emp_dep_data_obj AS OBJECT (
     empno             number(5),
     ename             varchar2(100),
     hiredate          DATE,
     sal               number
);
/
-- create Table Type of Object Type
CREATE OR REPLACE TYPE hr_emp_dep_data_tbl AS TABLE OF hr_emp_dep_data_obj;
/
-- ptf = Pipelined Table Function
CREATE OR REPLACE FUNCTION hr_emp_dep_ptf (p_dept in number) 
 RETURN hr_emp_dep_data_tbl PIPELINED
 IS
 v_dep_table hr_emp_dep_data_obj;
BEGIN
for rec in (select empno, ename, hiredate, sal
             from scott.emp
            where deptno = p_dept
)
loop
   PIPE ROW(hr_emp_dep_data_obj(
                  rec.empno, rec.ename, rec.hiredate, rec.sal));
end loop;
END hr_emp_dep_ptf;
select rownum, empno, ename, hiredate, sal 
 from table(hr_emp_dep_ptf(30)) order by 1;

regards

Comments
Post Details
Added on Aug 19 2024
4 comments
795 views