sql server 获取指定节点的所有父节点或者所有子节点

sql 获取指定节点的所有父节点或者所有子节点

获取节点的所有父节点
;with 
#tmp   as( 
	select   *   from   tb
	where  id   =  'DMA20120327036'
	union   all 
	select   a.*   from   tb a,   #tmp   b 
	where   a.id   =   b.pid
) 
select   *   from   #tmp

获取节点的所有子节点
;with 
#tmp   as( 
	select   *   from   tb
	where  id   =  'DMA20120327036'
	union   all 
	select   a.*   from   tb a,   #tmp   b 
	where   a.pid   =   b.id
) 
select   *   from   #tmp

猜你喜欢

转载自raozhiyong11.iteye.com/blog/1889182
今日推荐