create or replace type type_varc is table of varchar2(1000);
CREATE OR REPLACE FUNCTION f_str2tab(p_str IN VARCHAR2 --传入的字符串,格式为1,2,3,4,5
) RETURN type_varc IS
/******************************************************************
Ver1.0 Created by xsb on 2008-04-14
把字符串(1,2,3,4,5)转换为内存表形式
create or replace type type_varc is table of varchar2(1000);
测试用例:SELECT * FROM TABLE(f_str2tab('a,s,d,12,3,4,5'));
******************************************************************/
v_str VARCHAR2(4000) := p_str || ',';
v_cnt NUMBER := length(v_str) - length(REPLACE(v_str, ','));
v_numtab type_varc := type_varc(); --返回内存表
BEGIN
FOR i IN 1 .. v_cnt LOOP
v_numtab.EXTEND;
v_numtab(i) := substr(v_str, 1, instr(v_str, ',') - 1);
dbms_output.put_line('v_str:'||v_str);
dbms_output.put_line('v_numtab(i):'||v_numtab(i));
v_str := substr(v_str, instr(v_str, ',') + 1);
dbms_output.put_line('v_str:'||v_str);
END LOOP;
RETURN v_numtab;
EXCEPTION
WHEN OTHERS THEN
v_numtab.DELETE;
END;
--上面的可能会有重复的元素出现
select distinct * from TABLE(f_str2tab('a,s,d,12,12,3,4,5'))
使用此内存表关联时,需要加hint,如:
select /*+ ordered use_nl(a,b)*/ b.*
from TABLE(f_str2tab('a,s,d,12,3,4,5')) a,t1 b
where a.column_value=b.id1;