0
点赞
收藏
分享

微信扫一扫

oracle中生成树中每个节点对应其所有子孙的映射


这本是网友的一个问题。用了比较愚笨的方法解决的。抛砖引玉。

问题如下:

 有两张表如下
表1是原始表,表2的内容是把上级和下级的全部对应起来,包括自己,
比如表1上级2对应的下级有30,20,然后30的下级又有300
在表2中就要写4条记录,分别为30,20,300,2这几个下级。

不知道大伙能听明白否

表1
上级    下级
1       20
2       30
3       10
2       20
30   300

表2
上级    下级
1   20
1   1
2   30
2   20
2   2
3   10
3   3
30  300
2   300
30   30

 

解决方法:

 




SQL 
  > 
    
  create 
    
  table 
   a(u  
  number 
  ( 
  3 
  ),l  
  number 
  ( 
  3 
  )) 
  2 
    ;

 
  Table 
   created

SQL 
  > 
    
  insert 
    
  into 
   a(u,l)  
  values 
  ( 
  1 
  , 
  20 
  );

 
  1 
   row inserted
SQL 
  > 
    
  insert 
    
  into 
   a(u,l)  
  values 
  ( 
  2 
  , 
  30 
  );

 
  1 
   row inserted

SQL 
  > 
    
  insert 
    
  into 
   a(u,l)  
  values 
  ( 
  3 
  , 
  10 
  );

 
  1 
   row inserted

SQL 
  > 
    
  insert 
    
  into 
   a(u,l)  
  values 
  ( 
  2 
  , 
  20 
  );

 
  1 
   row inserted

SQL 
  > 
    
  insert 
    
  into 
   a(u,l)  
  values 
  ( 
  30 
  , 
  300 
  );

 
  1 
   row inserted
SQL 
  > 
    
  commit 
  ;

 
  Commit 
   complete

 
  1 
  ,创建类型

 
  create 
    
  or 
    
  replace 
   type r_type  
  is 
   object(u  
  number 
  ,l  
  number 
  );
 
  create 
    
  or 
    
  replace 
   type t_table  
  is 
    
  table 
     
  of 
   r_type;

2 ,创建function

oracle中生成树中每个节点对应其所有子孙的映射_sql

create 
    
  or 
    
  replace 
     
  function 
   getData  
  return 
   t_table
     
  as 
  
      
  cursor 
   ca  
  is 
    
  select 
    
  distinct 
   u  
  from 
   a;
     rs ca 
  % 
  rowtype;
      
  cursor 
   cb(kk  
  number 
  )  
  is 
    
  select 
    
  * 
    
  from 
   a connect  
  by 
   prior l 
  = 
  u start  
  with 
   u 
  = 
  kk; 
     rr cb 
  % 
  rowtype;   
     rs2 t_table: 
  = 
  t_table();     
     liu  
  number 
  ;
     i  
  integer 
  ;
      
  begin 
  
         i: 
  = 
  1 
  ;
          
  open 
   ca;
         loop
             
  fetch 
   ca  
  into 
   rs;
             
  exit 
    
  when 
   ca 
  % 
  notfound;
             
  open 
   cb(rs.u);
            loop
                
  fetch 
   cb  
  into 
   rr;
                
  exit 
    
  when 
   cb 
  % 
  notfound;
                  rs2.extend( 
  1 
  );
                  rs2(i): 
  = 
   r_type(rs.u,rr.l);
                  i: 
  = 
  i 
  + 
  1 
  ;
             
  end 
   loop;
            rs2.extend( 
  1 
  );
            rs2(i): 
  = 
   r_type(rs.u,rs.u);
            i: 
  = 
  i 
  + 
  1 
  ;
             
  close 
   cb;
          
  end 
   loop;
          
  close 
   ca;
          
  return 
   rs2;
    
  end 
  ;

3 ,查询验证

SQL 
  > 
    
  select 
    
  * 
    
  from 
    
  table 
  (getdata);

         U          L
 
  -- 
  -------- ---------- 
  
 
           
  30 
           
  300 
  
         
  30 
            
  30 
  
          
  1 
            
  20 
  
          
  1 
             
  1 
  
          
  2 
            
  30 
  
          
  2 
           
  300 
  
          
  2 
            
  20 
  
          
  2 
             
  2 
  
          
  3 
            
  10 
  
          
  3 
             
  3 
  

 
  10 
   rows selected


举报

相关推荐

0 条评论