下面是用 Pipelined Table 实现 split 函数的例子:
CREATE OR REPLACE TYPE ty_str_split IS TABLE OF VARCHAR2 (4000); CREATE OR REPLACE FUNCTION fn_split (p_str IN VARCHAR2, p_delimiter IN VARCHAR2) RETURN ty_str_split PIPELINED IS j INT := 0; i INT := 1; len INT := 0; len1 INT := 0; str VARCHAR2 (4000); BEGIN len := LENGTH (p_str); len1 := LENGTH (p_delimiter); WHILE j= len THEN EXIT; END IF; ELSE str := SUBSTR (p_str, i, j - i); i := j + len1; PIPE ROW (str); END IF; END LOOP; RETURN; END fn_split;
**测试:SELECT * FROM TABLE (fn_split ('1;;12;;123;;1234;;12345', ';;'));
结果:
1 12 123 1234 12345
又一个简单的例子:
CREATE TYPE mytype AS OBJECT ( field1 NUMBER, field2 VARCHAR2 (50) ); CREATE TYPE mytypelist AS TABLE OF mytype; CREATE OR REPLACE FUNCTION pipelineme RETURN mytypelist PIPELINEDIS v_mytype mytype; BEGIN FOR v_count IN 1 .. 20 LOOP v_mytype := mytype (v_count, 'Row ' || v_count); PIPE ROW (v_mytype); END LOOP; RETURN; END pipelineme;
SELECT * FROM TABLE (pipelineme);
FIELD1 FIEL