码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver with as 双向递归

时间:2015-05-26 12:19:50      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

递归上下级部门,获取相关的部门单位

  1. 部门表:技术分享
  2. sql语句:
    with Temp1 as
    (
    select GroupID,G_ParentID from sys_Group  where GroupID={当前部门GroupID}
    union all
    select sys_Group.GroupID,sys_Group.G_ParentID from Temp1  inner join sys_Group on Temp1.G_ParentID=sys_Group.GroupID 
      ),
    Temp2 as
     (
    select GroupID,G_ParentID from sys_Group  where GroupID={当前部门GroupID}
    union all
    select sys_Group.GroupID,sys_Group.G_ParentID from Temp2  inner join sys_Group on Temp2.GroupID=sys_Group.G_ParentID 
     )
    select GroupID from  Temp1 union  select GroupID from Temp2
    

     

sqlserver with as 双向递归

标签:

原文地址:http://www.cnblogs.com/ybyi/p/4530200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!