create or replace type str2tblType as table of varchar2(4000);
CREATE OR REPLACE FUNCTION str2tbl (p_str IN CLOB,
p_delim IN VARCHAR2 DEFAULT ',')
RETURN str2tbltype
AS
l_str CLOB := p_str || p_delim;
l_n NUMBER;
l_data str2tbltype := str2tbltype ();
BEGIN
LOOP
l_n := INSTR (l_str, p_delim);
EXIT WHEN (NVL (l_n, 0) = 0);
l_data.EXTEND;
l_data (l_data.COUNT) := LTRIM (RTRIM (SUBSTR (l_str, 1, l_n - 1)));
l_str := SUBSTR (l_str, l_n + 1);
END LOOP;
RETURN l_data;
END;
/
-------------------------------------实现--------------------------------------
DECLARE
v CLOB;
r str2tblType;
BEGIN
v := LPAD('A',4000)||','||LPAD('B',4000);
r := str2tbl(v);
FOR i IN 1..r.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(r(i));
END LOOP;
END;
/
or
select * from table(cast(str2tbl(LPAD(to_clob('A'),4000)||','||LPAD(to_clob('B'),4000)) as str2tblType));
以下写法是错误的如果传入长度大于4000,传入的非Clob 类型.
select * from table(cast(str2tbl(LPAD('A',4000)||','||LPAD('B',4000)) as str2tblType));