SQL with etc获取父节点或子节点总结

View Code
 1 获取节点的所有父节点
 2 ;with 
 3 #tmp   as( 
 4 select   *   from   tb
 5 where  id   =  'DMA20120327036'
 6 union   all 
 7 select   a.*   from   tb a,   #tmp   b 
 8 where   a.id   =   b.pid
 9 ) 
10 select   *   from   #tmp
11 
12 获取节点的所有子节点
13 ;with 
14 #tmp   as( 
15 select   *   from   tb
16 where  id   =  'DMA20120327036'
17 union   all 
18 select   a.*   from   tb a,   #tmp   b 
19 where   a.pid   =   b.id
20 ) 
21 select   *   from   #tmp
22 
23 搞好好久总结出来的……

 

posted on 2012-04-19 01:08  comcyd  阅读(273)  评论(0编辑  收藏  举报

导航