X

Katsumi's Blog

SQL: Replacing "CONNECT BY" with recursive subquery

In my previous post, I used "CONNECT BY" clause.

SQL: Delimited list of values as in-line view. csv on memory (INOUE Katsumi @ Tokyo)

select regexp_substr('KING,TURNER,BLAH,!ERROR!,0,JAMES','[^,]+',1,level) as token
from dual connect by level <= 256

But after reading very informative blog page below, I decided to rewrite it.

About Oracle: Recursive subquery factoring

Version 11g release 2 introduced recursive subquery factoring or the recursive with clause.

Here's my result. I stared at this SQL for 10 minutes but I didn't come up with any smarter one.
Of course, I may be wrong.

SQL> var string varchar2(256)
SQL> exec :string := 'aa,b_b,cc cc,dddd:ddd'
PL/SQL procedure successfully completed.
SQL> with tokens(token, level_num) as2 (select regexp_substr(:string,'[^,]+',1,1),1 from dual3 union all4 select regexp_substr(:string,'[^,]+',1,level_num+1),level_num+1 from tokens5 where level_num < 256)6 select token from tokens where token is not null7 /
aa
b_b
cc cc
dddd:ddd

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.