用户登录
用户注册

分享至

oracle管道函数的用法(一行拆为多行)

  • 作者: 无证斋
  • 来源: 51数据库
  • 2021-10-19

oracle管道函数是一类特殊的函数,oracle管道函数返回值类型必须为集合

如果需要在客户端实时的输出函数执行过程中的一些信息,在oracle9i以后可以使用管道函数(pipeline function)。

关键字pipelined表明这是一个oracle管道函数,oracle管道函数的返回值类型必须为集合

--创建一个集合接受返回的值

1st.create or replace type type_split as table of varchar2(4000);

 

--创建管道函数

 

create or replace function split(p_string varchar2, p_sep varchar2 := ',') return type_split pipelined
--dbms_output输出的信息,需要在服务器执行完整个函数后一次性的返回给客户端
--pipelined 表明这是一个管道函数,oracle管道函数的返回值类型必须为集合
--pipe row语句被用来返回该集合的单个元素
as

v_string varchar2(4000) := p_string;
idx number;

begin
loop
--idx为第一个,所在的位置
idx := instr(v_string, p_sep);
if idx > 0 then
--,前面的数据加入row/,后面的数据为下个循环使用的字符串
pipe row(substr(v_string, 1, idx - 1));
v_string := substr(v_string, idx + length(p_sep));
else
exit;
end if;
end loop;
--执行完后需return
return ;
end;

 

 

test:

 

select a.cust_po,b.column_value proqepi from
(
  select cust_po,proqepi

  from cux_custpo_info_t

  where cust_po='px90806001-4'
) a,(table(split(a.proqepi,','))) b

 

测试成功。

 

软件
前端设计
程序设计
Java相关