函数返回一个表

时间:2013-03-28 09:24:41

标签: plsql oracle11g

这里我有一个函数,它接受'COMMA SEPARATED STRING'并返回一个流水线表。

  create or replace 
  FUNCTION parse_comma_delimited
   (
     iv_list IN VARCHAR2,
     v_delimiter IN VARCHAR2 DEFAULT ',' 
   )
    RETURN parse_comma_delimited_pkg.tt_v_tablevalues_type PIPELINED
     AS
       v_list VARCHAR2(8000) := iv_list;
      v_item VARCHAR2(255);
      v_temp SYS_REFCURSOR;
       v_temp_1 TT_V_TABLEVALUES%ROWTYPE;
   BEGIN
    WHILE ( LENGTHB(v_list) > 0 ) 
     LOOP 
     BEGIN
     IF INSTR(v_list, v_delimiter) > 0 THEN
     BEGIN
        v_item := SUBSTR(v_list, 1, (INSTR(v_list, v_delimiter) - 1)) ;
        v_list := SUBSTR(v_list, (INSTR(v_list, v_delimiter) 
      || LENGTHB(v_delimiter)), LENGTHB(v_list)) ;
     END;
     ELSE
     BEGIN
        v_item := v_list ;
        v_list := NULL ;
     END;
     END IF;

     INSERT INTO tt_v_tablevalues ( item ) VALUES (v_item);
  END;
END LOOP;
OPEN v_temp FOR
  SELECT * 
    FROM tt_v_tablevalues;
  LOOP
  FETCH v_temp INTO v_temp_1;
  EXIT WHEN v_temp%NOTFOUND;
  PIPE ROW ( v_temp_1 );
  END LOOP;
END;

但是当我调用该函数时,它只重新调整一行。

INSERT INTO SRC_PK_INSERT (pk_key)
SELECT * 
FROM TABLE(parse_comma_delimited(( 
SELECT * 
FROM SRC_PK_INSERT
WHERE RULE_NAME = 'RULES'
))); 

1 个答案:

答案 0 :(得分:0)

使用流水线功能填充row_type,然后调用管道行。

请参阅此处的文档:http://docs.oracle.com/cd/B28359_01/appdev.111/b28425/pipe_paral_tbl.htm#CHDJEGHC

docs中的示例:

CREATE 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;
    -- first row
    out_rec.ticker := in_rec.Ticker;
    out_rec.PriceType := 'O';
    out_rec.price := in_rec.OpenPrice;
    PIPE ROW(out_rec);
    -- second row
    out_rec.PriceType := 'C';   
    out_rec.Price := in_rec.ClosePrice;
    PIPE ROW(out_rec);
  END LOOP;
  CLOSE p;
  RETURN;
END;
/