关于 pipe row的用法2009/12/30 14:53
发表于:2007.11.28 11:14
分类: oracle10g
出处:http://flysky0814.itpub.net/post/35477/419443
---------------------------------------------------------------
管道化表函数必须返回一个集合。在函数中,PIPE ROW 语句被用来返回该集合的单个元素,该函数必须以一个空的 RETURN 语句结束,以表明它已经完成。一旦我们创建了上述函数,我们就可以使用 TABLE 操作符从 SQL 查询中调用它。
管道化表函数经常被用来把数据从一种类型转化成另一种类型。
下面是用 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
LOOP
j := INSTR (p_str, p_delimiter, i);
IF j = 0
THEN
j := len;
str := SUBSTR (p_str, i);
PIPE ROW (str);
IF i >= 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 FIELD2
------ ------------------------
1 Row 1
2 Row 2
3 Row 3
4 Row 4
5 Row 5
6 Row 6
7 Row 7
8 Row 8
9 Row 9
10 Row 10
11 Row 11
12 Row 12
13 Row 13
14 Row 14
15 Row 15
16 Row 16
17 Row 17
18 Row 18
19 Row 19
20 Row 20
create or replace type dlsys.split_tbl as table of varchar2(32767);
create or replace function dlsys.dlsplit(p_list varchar2,
p_del varchar2 := ',')
return dlsys.split_tbl
pipelined is
l_idx pls_integer;
l_list varchar2(32767) := p_list;
begin
loop
l_idx := instr(l_list, p_del);
if l_idx > 0 then
pipe row(substr(l_list, 1, l_idx - 1));
l_list := substr(l_list, l_idx + length(p_del));
else
pipe row(l_list);
exit;
end if;
end loop;
return;
end dlsplit;
-----------------------------------------------------------------------------------------------
DECLARE
a ty_str_split ;
BEGIN
select fn_split ('1;;12;;123;;1234;;12345;;w', ';;') into a from dual ;
a(1) := 'ell';
dbms_output.put_line(a(100));
null;
END;