oracle中使用SQL递归语句

例子:

 pid  id
  a   b  
  a   c    
  a   e  
  b   b1  
  b   b2  
  c   c1  
  e   e1  
  e   e3  
  d   d1  
   
  指定pid=a,选出  
  a   b  
  a   c    
  a   e  
  b   b1  
  b   b2  
  c   c1  
  e   e1  
  e   e3 
SQL语句:select   parent,child   from   test   start   with   pid='a'   
connect   
by   prior   id=pid 

 

 

Oracle  SQL的递归查询:
1、表机构
SQL> desc comm_org_subjection
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ORG_SUBJECTION_ID                         NOT NULL VARCHAR2(32)    子键
 ORG_ID                                    NOT NULL VARCHAR2(32)
 FATHER_ORG_ID                             NOT NULL VARCHAR2(32)         父键
 LOCKED_IF                                 NOT NULL VARCHAR2(1)
 START_DATE                                NOT NULL DATE
 END_DATE                                           DATE
 EDITION_NAMEPLATE                                  NUMBER(8)
 CODE_AFFORD_IF                                     VARCHAR2(1)
 CODE_AFFORD_ORG_ID                        NOT NULL VARCHAR2(32)
 CODING_SHOW_ID                                     NUMBER(8)
 BSFLAG                                             VARCHAR2(1)
 MODIFI_DATE                                        DATE
 CREATOR_ID                                         VARCHAR2(32)
 CREATE_DATE                                        DATE
 CREATOR                                            VARCHAR2(35)


2、递归查找父结点 org_id为C6000000000001下的所有子结点:
select * from comm_org_subjection a
start with a.org_id='C6000000000001'
connect by prior a.org_subjection_id=a.father_org_id


3、递归查找子结点 org_id为C6000000000001下的所有父结点:
select org_id from comm_org_subjection a
start with a.org_id='C6000000000001'
connect by prior a.father_org_id=a.org_subjection_id

 

posted on 2010-07-08 10:00  highmayor  阅读(224)  评论(0编辑  收藏  举报

导航