Thursday, November 23, 2017

Converting a Cooma Seperated List to a 'Oracle DB TABLE TYPE collections'

Here input string is 'Apple,  Orange   ,Mango,Grapes' and output is a table as test_table


create or replace TYPE test_type AS TABLE OF VARCHAR2(100);

DECLARE
test_table test_type;
v_counter INTEGER := 0;
BEGIN
 SELECT ltrim(rtrim(regexp_substr('Apple,  Orange   ,Mango,Grapes','[^,]+',1,level))) regexp_substr
 BULK COLLECT INTO test_table FROM dual CONNECT BY level<=regexp_count('Apple,  Orange   ,Mango,Grapes',',')+1;


--Check the data

FOR i in 1..test2.COUNT
LOOP
SYS.DBMS_OUTPUT.PUT_LINE(test_table(i));
END LOOP;

end;