標籤:
PL/SQL 中沒有split函數,需要自己寫。
代碼:
create or replace type type_split as table of varchar2(50); --建立一個 type ,如果為了使split函數具有通用性,請將其size 設大些。
--建立function
create or replace function split
(
p_list varchar2,
p_sep varchar2 := ‘,‘
) return type_split pipelined
is
l_idx pls_integer;
v_list varchar2(50) := p_list;
begin
loop
l_idx := instr(v_list,p_sep);
if l_idx > 0 then
pipe row(substr(v_list,1,l_idx-1));
v_list := substr(v_list,l_idx+length(p_sep));
else
pipe row(v_list);
exit;
end if;
end loop;
return;
end split;
測試:
SQL> select * from table(split(‘northsnow,塞北的雪‘,‘,‘));
COLUMN_VALUE
--------------------------------------------------
northsnow
塞北的雪
SQL>
補充:
-----PIPELINED關鍵字是什麼意思?
---- pipe row是什麼意思?
pipelined聲名此function是pipe的,如果這麼聲名了,就必須使用pipe row的方式把資料返回,常規函數最後的"return 變數",就變成了"return".
pipelined的function主要是為了提高效率,不用等所有的資料都處理完成了才返回用戶端,它是邊處理邊返回.適用於大資料量的互動.
oracle的split函數