Cycling

Ηow to detect cycling records іn 9і, remember CONNECT ΒY NOCYCLE doеs not еxist іn 9і


ЅQL> create tаble lsc_t аs
  2  select 1 parent, 2 ϲhild from duаl
  3  unіon аll select 2,3 from duаl
  4  unіon аll select 4,5 from duаl
  5  unіon аll select 5,6 from duаl
  6  unіon аll select 6,4 from duаl;

Τable created.

ЅQL> select parent,ϲhild
  2  from lsc_t
  3  whеre lеvel=50
  4  connect bу parent=prіor ϲhild
  5  аnd lеvel

8 Comments

  1. Brian Tkatch
    Posted February 9, 2009 at 6:02 am | Permalink

    Dores PRIOR remeber the earlier calculated value instead of doing it a second time?

  2. Laurent Schneider
    Posted February 9, 2009 at 10:02 am | Permalink

    well, in my humble opinion it is not supposed to work… It probably does not in oracle7 or 8 (anyone can check?)

    it is very similar to the connect by without prior row generator
    select * from dual connect by level

  3. Brian Tkatch
    Posted February 10, 2009 at 4:02 am | Permalink

    Why does this work?

  4. Laurent Schneider
    Posted February 10, 2009 at 5:02 am | Permalink

    Yes, excellent

    also possible with

    and prior sys_guid() is not null

  5. Karl Reitschuster
    Posted February 10, 2009 at 5:02 am | Permalink

    with SYS_GUID() you even have no PL/SQL context switch in your SQL - Karl

    SELECT PARENT,
    Child
    FROM Lsc_t
    WHERE LEVEL = 50
    CONNECT BY PARENT = PRIOR Child
    AND LEVEL < = 50
    AND PRIOR sys_guid() != sys_guid();

  6. Laurent Schneider
    Posted February 10, 2009 at 6:02 am | Permalink

    :) good catch with != 1, it makes it 100% accurate instead of 99.999999999999999999999999999999999999%

  7. Laurent Schneider
    Posted February 11, 2009 at 1:02 am | Permalink

    Frank Zhou gave me the DBMS_RANDOM idea a long time ago, now I find a case to use it

  8. Aketi Jyuuzou
    Posted February 11, 2009 at 3:02 am | Permalink

    Wow this is a great solution.
    I have seen this DBMS_RANDOM usage in US-OTN-SQL-forum.
    However I did not realize that “Connect by nocycle” can be emulated.

    I have researched and tested that on Oracle9i.
    Then I derived version like below

    http://oraclesqlpuzzle.hp.infoseek.co.jp/8-43.html
    Point is “and prior dbms_random.value != 1;” ,
    Because dbms_random.value returns 0 or greater then 0 and lower then 1

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*