1. 程式人生 > 資料庫 >詳解oracle管道函式的用法(一行拆為多行)

詳解oracle管道函式的用法(一行拆為多行)

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

測試成功。

總結

以上所述是小編給大家介紹的oracle管道函式的用法(一行拆為多行),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!