Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, John.

Asked: October 30, 2015 - 10:02 am UTC

Last updated: November 02, 2015 - 3:40 am UTC

Version: 11.2.0.4

Viewed 1000+ times

You Asked

Hi,
I quite often use this technique for tokenizing a string into rows:

With tst As (  
Select 'row1' r, 'Hello,World,Test' d From Dual  
)  
Select r, Regexp_Substr(d,'[^,]+',1,level) From tst  
Connect By Instr(d,',',1,level-1) > 0;  


however if I have multiple values for r, i.e:

With tst As (  
Select 'row1' r, 'Hello,World,Test' d From Dual  
Union All  
Select 'row2' r, 'Some,Other,Row' d From Dual  
)  
Select r, Regexp_Substr(d,'[^,]+',1,level) From tst  
Connect By Instr(d,',',1,level-1) > 0; 


then I get duplicate values rreturned. If I put in And Prior v.r = v.r then I get a cyclic warning.

Somebody suggested that I modify my query to the following:

With tst As (  
Select 'row1' r, 'Hello,World,Test' d From Dual  
Union All  
Select 'row2' r, 'Some,Other,Row' d From Dual  
)  
Select r, Regexp_Substr(d,'[^,]+',1,level) From tst  
Connect By Instr(d,',',1,level-1) > 0
And Prior r=r
And Prior sys_guid() Is Not Null;


Which works perfectly. However I really don't like using something without knowing Why this fixes the solution? From what I've read sys_guid() simply returns a unique identifier - so why does it eliminate the duplicate rows? Is this because using level in this way is effectively exploiting an unintended feature of the construct? I think you have alluded to that in the past in another post (and if that is indeed the issue then would you advise against using this method at all?)? Do we just need a function which returns a unique value for every row? If so then although you would of course never do it, the following would work most of the time.
And prior dbms_random.value() is not null?

Can you elaborate on this?

Many thanks,
John

and Connor said...

Nice description here

https://community.oracle.com/thread/2526535

Hope this helps

Is this answer out of date? If it is, please let us know via a Comment

More to Explore

DBMS_RANDOM

More on PL/SQL routine DBMS_RANDOM here