ORACLE 에서 CONNECT BY... START WITH... 의 사용

전송:http://www.cnblogs.com/baiyixianzi/archive/2012/08/30/plsql12.html
 、   
    :select * from some_table [where   1] connect by [  2] start with [  3]; 
   connect by   start with                  ,[where   1]     。 
[where   1]、[  2]、[  3]           : 

[where   1]    “connect by [  2] start with [  3]”            ,          ,         ; 

[  2]        ,           ,                              ; 

[  3]            ,                      ,                       ; 

  : 
         :some_table(id,p_id,name),  p_id      id。 
select * from some_table t where t.id!=123 connect by prior t.p_id=t.id and t.p_id!=321 start with t.p_id=33 or t.p_id=66; 

 prior   : 
    prior   [  2] ,    ,            “start with [  3]”   ,             。         :connect by prior t.p_id=t.id   connect by t.p_id=prior t.id,                  (           ),                  (            )。 

 、     
connect by...start with...                        RECURSE()      : 

/*          ,      start with    ,           , 
             ,    RECURSE()            , 
                  。*/ 
for rec in (select * from some_table) loop 
if FULLFILLS_START_WITH_CONDITION(rec) then 
    RECURSE(rec, rec.child); 
end if; 
end loop; 

/*           */ 
procedure RECURSE (rec in MATCHES_SELECT_STMT, new_parent IN field_type) is 
begin 
APPEND_RESULT_LIST(rec); /*          */ 
/*           ,      connect by    ,           , 
              ,    RECURSE()              , 
            。*/ 
for rec_recurse in (select * from some_table) loop 
    if FULLFILLS_CONNECT_BY_CONDITION(rec_recurse.child, new_parent) then 
      RECURSE(rec_recurse,rec_recurse.child); 
    end if; 
end loop; 
end procedure RECURSE; 

 、     
                “connect by...start with...”       :(1)         ,                    ,                   。(2)            ,                      ,                   。 
                                   ,                  ,                             
--1.Hierarchical Queries: START WITH and CONNECT BY PRIOR clauses 
--Hierarchical Queries
--START WITH and CONNECT BY PRIOR clauses.

SELECT employee_id, manager_id, first_name, last_name
FROM employee_jh
START WITH employee_id = 1
CONNECT BY PRIOR employee_id = manager_id;

EMPLOYEE_ID MANAGER_ID FIRST_NAME LAST_NAME
----------- ---------- ---------- ----------

          1          0 James      Smith
          2          1 Ron        Johnson
          3          2 Fred       Hobbs
          5          2 Rob        Green
          4          1 Susan      Jones
          6          4 Jane       Brown
          9          6 Henry      Heyson
          7          4 John       Grey
          8          7 Jean       Blue
         10          1 Kevin      Black
         11         10 Keith      Long
         12         10 Frank      Howard
         13         10 Doreen     Penn

13 rows selected.

--2.Using a Subquery in a START WITH Clause
SELECT LEVEL,
       LPAD(' ', 2 * LEVEL - 1) || first_name || ' ' || last_name AS employee
FROM employee_jh
START WITH employee_id = (SELECT employee_id FROM employee_jh WHERE first_name = 'Kevin' AND last_name = 'Black')
CONNECT BY PRIOR employee_id = manager_id;

     LEVEL EMPLOYEE
---------- -------------------------
         1  Kevin Black
         2    Keith Long
         2    Frank Howard
         2    Doreen Penn

--3.Including Other Conditions in a Hierarchical Query
SELECT LEVEL,
       LPAD(' ', 2 * LEVEL - 1) || first_name || ' ' ||
       last_name AS employee, salary
FROM employee_jh
WHERE salary <= 50000
START WITH employee_id = 1
CONNECT BY PRIOR employee_id = manager_id;

     LEVEL EMPLOYEE                      SALARY
---------- ------------------------- ----------
         3      Rob Green                 40000
         3      Jane Brown                45000
         4        Henry Heyson            30000
         3      John Grey                 30000
         4        Jean Blue               29000
         3      Keith Long                50000
         3      Frank Howard              45000
         3      Doreen Penn               47000

8 rows selected.



: http://zhidao.baidu.com/question/219362803.html

좋은 웹페이지 즐겨찾기