Recursive Sub-Query Factoring using the WITH Clause (A.K.A. Common Table Expressions)
Splitting a Delimited String
Section titled “Splitting a Delimited String”Sample Data:
CREATE TABLE table_name ( value VARCHAR2(50) );
INSERT INTO table_name ( value ) VALUES ( 'A,B,C,D,E' );Query:
WITH items ( list, item, lvl ) AS ( SELECT value, REGEXP_SUBSTR( value, '[^,]+', 1, 1 ), 1 FROM table_nameUNION ALL SELECT value, REGEXP_SUBSTR( value, '[^,]+', 1, lvl + 1 ), lvl + 1 FROM items WHERE lvl < REGEXP_COUNT( value, '[^,]+' ))SELECT * FROM items;Output:
LIST ITEM LVL--------- ---- ---A,B,C,D,E A 1A,B,C,D,E B 2A,B,C,D,E C 3A,B,C,D,E D 4A,B,C,D,E E 5A Simple Integer Generator
Section titled “A Simple Integer Generator”Query:
WITH generator ( value ) AS ( SELECT 1 FROM DUALUNION ALL SELECT value + 1 FROM generator WHERE value < 10)SELECT valueFROM generator;Output:
VALUE----- 1 2 3 4 5 6 7 8 9 10Remarks
Section titled “Remarks”Recursive sub-query factoring is available in Oracle 11g R2.