您的位置:首页 > 数据库

sql语句递归查询

2012-03-29 16:36 309 查看
代码如下:

View Code

with temp(nodeid,nodename,parentid) as
(
select ou.nodeid,ou.nodename,ou.parentid from OU ou where ou.parentid=(select parentid from OU where nodeid ='010201')and  ou.nodeid='010201'
union all
select ou.nodeid,ou.nodename,ou.parentid from OU ou,temp tm where ou.nodeid=tm.parentid

)
select * from temp
go
注:ou.nodeid=tm.parentid是从本部门向上递归(即查出本部门以上的部门包括本部门)
ou.parentid=tm.nodeid是从本部门向上递归(即查出本部门以下的部门包括本部门)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: