This is a small demonstartion of how to insert the values of a comma separated string to a table as different rows. This is not something new, Tom Kyte has already demonstrated it lot of times in AskTom. However, I just want to keep it simple, just to insert values of a comma separated string into a table.
Let’s see the demo :
SQL> SET SERVEROUTPUT ON;
SQL> CREATE TABLE TEST (COL1 VARCHAR2(10))
2 /
Table created.
SQL> DECLARE
2 L_INPUT VARCHAR2(4000) := 'THIS,IS,A,COMMA,SEPARATED,STRING';
3 L_COUNT BINARY_INTEGER;
4 L_ARRAY DBMS_UTILITY.LNAME_ARRAY;
5 BEGIN
6 DBMS_UTILITY.COMMA_TO_TABLE(LIST => REGEXP_REPLACE(L_INPUT, '(^|,)', '\1x'), TABLEN => L_COUNT, TAB => L_ARRAY);
7 DBMS_OUTPUT.PUT_LINE(L_COUNT);
8 FOR I IN 1 .. L_COUNT
9 LOOP
10 DBMS_OUTPUT.PUT_LINE('Element ' || TO_CHAR(I) || ' of array contains: ' || SUBSTR(L_ARRAY(I), 2));
11 INSERT INTO TEST VALUES
12 (SUBSTR(L_ARRAY(I), 2)
13 );
14 COMMIT;
15 END LOOP;
16 END;
17 /
6
Element 1 of array contains: THIS
Element 2 of array contains: IS
Element 3 of array contains: A
Element 4 of array contains: COMMA
Element 5 of array contains: SEPARATED
Element 6 of array contains: STRING
PL/SQL procedure successfully completed.
SQL> SELECT * FROM TEST
2 /
COL1
----------
THIS
IS
A
COMMA
SEPARATED
STRING
6 rows selected.
SQL>
Hope it helps!
Filed under: Oracle Delimited String manipulation Tagged: comma seperated string, comma to table, Insert Comma seperated string values into Table, split string, string to table
