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是从本部门向上递归(即查出本部门以下的部门包括本部门)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM