13.05.2013 Views

Thanks for the question regarding "connect by ", versi

Thanks for the question regarding "connect by ", versi

Thanks for the question regarding "connect by ", versi

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

December 9, 2003 - 2am Central time zone Bookmark | Bottom | Top<br />

Reviewer: umesh from blore<br />

SQL> l<br />

1 SELECT<br />

2 DECODE ( ll ,1 , crit1.name ) one ,<br />

3 DECODE ( ll ,2 , crit1.name) two ,<br />

4 DECODE ( ll ,3 , crit1.name) three ,<br />

5 DECODE ( ll ,4 , crit1.name) four<br />

6 FROM<br />

7 (SELECT sc_id_child , sc_id_parent , LEVEL ll FROM CNT_SEARCH_HIERARCHY src<br />

8 START WITH sc_id_parent='crite000000000000002'<br />

9 CONNECT BY PRIOR sc_id_child=sc_id_parent<br />

10 ) , CNT_SEARCH_CRITERIA crit , CNT_SEARCH_CRITERIA crit1<br />

11 WHERE crit.id = sc_id_parent<br />

12* AND crit1.id=sc_id_child<br />

SQL> /<br />

ONE TWO THREE FOUR<br />

-------------------------- --------------------------- ------------------------<br />

-------------------<br />

Building Automation<br />

ACSELON<br />

Authorized Trainer<br />

Programs<br />

Delivery & Installation<br />

ADEPT<br />

Project<br />

Management<br />

Human Resources<br />

Love it, but ... March 4, 2004 - 3pm Central time zone Bookmark | Bottom | Top<br />

Reviewer: Peter Tran from Houston, TX USA<br />

Hi Tom,<br />

What happens if you have a dataset where <strong>the</strong> top mgr refers to himself? Using your example, if <strong>the</strong><br />

MGR = 7839 <strong>for</strong> KING and I run <strong>the</strong> same query you have Oracle gives me a:<br />

ORA-01436: CONNECT BY loop in user data<br />

A <strong>connect</strong> <strong>by</strong> record cannot reference itself o<strong>the</strong>rwise we have an infinite loop situation.<br />

Is <strong>the</strong>re anyway I can filter this out? In o<strong>the</strong>r words, I still want <strong>the</strong> answer you got earlier,<br />

but now I have <strong>the</strong> situation where KING refers to himself in <strong>the</strong> MGR column.<br />

<strong>Thanks</strong>,<br />

-Peter<br />

Followup March 4, 2004 - 3pm Central time zone:<br />

that case is easy -- since we have <strong>the</strong> "loop" in a single row, we can simply filter it out in <strong>the</strong><br />

"<strong>connect</strong> <strong>by</strong>" clause:<br />

ops$tkyte@ORA9IR2> create table emp as select * from scott.emp;<br />

Table created.<br />

ops$tkyte@ORA9IR2> update emp set mgr = empno where mgr is null;<br />

1 row updated.<br />

ops$tkyte@ORA9IR2> commit;<br />

Commit complete.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!