Oracle Pipelined Table Functions

Oracle Pipelined Table Functions

 
 

Version 10.2


General

Data dictionary Objects

source$
dba_proceduresall_proceduresuser_procedures
dba_sourceall_sourceuser_source
dba_typesall_typesuser_types
System Privilegesalter any procedure
create any procedure
create procedure
debug any procedure
drop any procedure
execute any procedure

select any table
 
Tables And Data For Demo
Table DefinitionCREATE TABLE stocktable (
ticker       VARCHAR2(4),
open_price   NUMBER(10),
close_price  NUMBER(10));
Demo DataINSERT INTO stocktable VALUES ('ORA', 13, 14);
INSERT INTO stocktable VALUES ('MSC', 85, 92);
INSERT INTO stocktable VALUES ('SUN', 23, 18);
COMMIT;
Type DefinitionCREATE OR REPLACE TYPE TickerType AS OBJECT (
ticker     VARCHAR2(4),
pricetype  VARCHAR2(1),
price      NUMBER(10));
/
Create Table TypeCREATE OR REPLACE TYPE TickerTypeSet AS TABLE OF TickerType;
/
Create PackageCREATE OR REPLACE PACKAGE refcur_pkg IS

TYPE refcur_t IS REF CURSOR RETURN StockTable%ROWTYPE;

END refcur_pkg;
/

Create Table Function
CREATE OR REPLACE FUNCTION stockpivot(
p refcur_pkg.refcur_t)
RETURN TickerTypeSet PIPELINED IS

out_rec  TickerType := TickerType(NULL,NULL,NULL);
in_rec   p%ROWTYPE;

BEGIN
  LOOP
    FETCH p INTO in_rec;
    EXIT WHEN p%NOTFOUND;

    out_rec.ticker := in_rec.Ticker;
    out_rec.pricetype := 'O';
    out_rec.price := in_rec.Open_Price;
    PIPE ROW(out_rec);

    out_rec.PriceType := 'C';
    out_rec.price := in_rec.Close_Price;
    PIPE ROW(out_rec);
  END LOOP;
  CLOSE p;
  RETURN;
END stockpivot;
/

desc stockpivot

col pipelined format a10

SELECT object_name, pipelined, authid
FROM user_procedures;
Sample QuerySELECT *
FROM TABLE(stockpivot(CURSOR(SELECT * FROM StockTable)));
 

Generate Date List
CREATE OR REPLACE TYPE date_array AS TABLE OF DATE;
/

CREATE OR REPLACE FUNCTION date_table(sdate DATE, edate DATE)
RETURN date_array PIPELINED AS

BEGIN
  FOR i IN 0 .. (edate - sdate)
  LOOP
    PIPE ROW(sdate + i);
  END LOOP;
  RETURN;
END date_table;
/

desc date_table

col pipelined format a10

SELECT object_name, pipelined, authid
FROM user_procedures;

SELECT *
FROM TABLE(CAST(date_table(TRUNC(SYSDATE-30), TRUNC(SYSDATE))
AS date_array));

-- joined with another table

CREATE TABLE testdata (
datecol  DATE,
someval  NUMBER);

INSERT INTO testdata VALUES (TRUNC(SYSDATE-25), 25);
INSERT INTO testdata VALUES (TRUNC(SYSDATE-20), 20);
INSERT INTO testdata VALUES (TRUNC(SYSDATE-15), 15);
INSERT INTO testdata VALUES (TRUNC(SYSDATE-10), 10);
INSERT INTO testdata VALUES (TRUNC(SYSDATE-5), 5);
COMMIT;

SELECT * FROM testdata;

SELECT da.column_value AS DATECOL, td.someval
FROM TABLE(CAST(date_table(TRUNC(SYSDATE-30), TRUNC(SYSDATE))
AS date_array)) da, testdata td
WHERE da.COLUMN_VALUE = td.datecol(+);
 

Turning a comma delmimited list into a row-by-row output
CREATE OR REPLACE TYPE str_array AS TABLE OF VARCHAR2(10);
/

CREATE OR REPLACE FUNCTION tf(stringin VARCHAR2) RETURN str_array PIPELINED IS
 i    PLS_INTEGER;
 str  VARCHAR2(100);
 tab  sys.dbms_utility.uncl_array;
BEGIN
  str := '"' || REPLACE(stringin, ',', '","') || '"';
  sys.dbms_utility.comma_to_table(str, i, tab);

  FOR j IN 1 .. 5 LOOP
    PIPE ROW(TRANSLATE(tab(j),'A"','A'));
  END LOOP;
  RETURN;
END tf;
/

SELECT *
FROM TABLE(CAST(tf('1001,1002,1003,1004,1005')
AS str_array));
 
Drop Pipelined Table Functions
Drop PTFDROP FUNCTION <function_name>;
DROP FUNCTION stockpivot;
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值