Wednesday, November 7, 2012

PIPELINED FUNCTION in ORCLE

WHAT IS PIPELINED FUNCTION:
Pipelined function is a function, which can be treated as a table.

WHY DO WE NEED IT:
As I know:
1. escape stage table in ETL process. DO ETL manipulation in piplined function and results can be extract directly from the function since the function is the result table.
2. provide different data according to parameter. Sometime you want to check data distributed in different scheams or tables. You need modify your query with different schema name and talbe name and other things,   which requires user know table structures column names bla bal. You can use pipelined function with simple parameter to provide a easy interface to pull out data.

e.x.

CREATE OR REPLACE FUNCTION GET_ERRSUM(SCHEMA_NAME VARCHAR2)
RETURN TEST_ERRSUM_TAB PIPELINED AS
TYPE REF0 IS REF CURSOR;
CUR0 REF0;
OUT_REC TEST_ERRSUM := TEST_ERRSUM(NULL,NULL,0);
DBNAME VARCHAR2(40);
BEGIN
SELECT DBNAME INTO OLAP_DB
FROM TESTAPPLICATION
WHERE TESTIDENTIFIER=SCHEMA_NAME;
OPEN CUR0 FOR 'select eh.pd, eh.cn, count(*) errcount
FROM '||DBNAME||'.eventerrorhistory eh where eh.pd = (select max(pd) from '||DBNAME||'.eventerrorhistory)
group by eh.pd, eh.cn order by pd desc, eh.cn desc';
LOOP
FETCH CUR0 INTO OUT_REC.pd, OUT_REC.cn, OUT_REC.errcount;
EXIT WHEN CUR0%NOTFOUND;
PIPE ROW(OUT_REC);
END LOOP;
CLOSE CUR0;
RETURN ;
END GET_ERRSUM;
/


reference:
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:19481671347143
http://www.akadia.com/services/ora_pipe_functions.html


No comments:

Post a Comment