sql语句递归查询

代码如下:

View Code
 1 with temp(nodeid,nodename,parentid) as
2 (
3 select ou.nodeid,ou.nodename,ou.parentid from OU ou where ou.parentid=(select parentid from OU where nodeid ='010201')and ou.nodeid='010201'
4 union all
5 select ou.nodeid,ou.nodename,ou.parentid from OU ou,temp tm where ou.nodeid=tm.parentid
6
7 )
8 select * from temp
9 go
10 注:ou.nodeid=tm.parentid是从本部门向上递归(即查出本部门以上的部门包括本部门)
11 ou.parentid=tm.nodeid是从本部门向上递归(即查出本部门以下的部门包括本部门)
原文地址:https://www.cnblogs.com/zcttxs/p/2423611.html