sql server 使用公共表表达式(CTE)递归

 1 with companyNodes(ParentNodeID,NodeID,NodeTypeID,level) as
 2 (
 3   select n.ParentNodeID,n.NodeID,n.NodeTypeID,0 level
 4   from tblclient_companyowner as o inner join tblSys_Node as n on o.NodeID=n.NodeID
 5   where o.CompanyID=238
 6   union all
 7   select n.ParentNodeID,n.NodeID,n.NodeTypeID,companyNodes.level+1
 8   from companyNodes inner join tblSys_Node n on companyNodes.NodeID=n.ParentNodeID
 9 )
10 
11 select * from companyNodes where NodeTypeID=4

 

posted @ 2014-10-21 16:11  LeonGo  阅读(247)  评论(0编辑  收藏  举报