select U.Id,U.Name,C.Id,C.Name,C.ParentId,C.Id,C.Name,C.Tree from [UserName] U 
inner join [User_Compay] UC on U.Id=UC.UserId 
inner join  [Compay] C on UC.CompayId=C.Id
Id          Name                                               Id          Name                                               ParentId    Id          Name                                               Tree
----------- -------------------------------------------------- ----------- -------------------------------------------------- ----------- ----------- -------------------------------------------------- -----------
1           杜玲利                                                3           总部-人事部                                             1           3           总部-人事部                                             1
2           唐红                                                 6           上海分公司-电脑部-电脑1部                                     5           6           上海分公司-电脑部-电脑1部                                     3
3           李小林                                                2           上海分公司                                              1           2           上海分公司                                              1
4           张光                                                 5           上海分公司-电脑部                                          2           5           上海分公司-电脑部                                          2
5           王涛                                                 4           上海分公司-销售部                                          2           4           上海分公司-销售部                                          2
6           贾云                                                 7           上海分公司-电脑部-电脑1部-精英组                                 6           7           上海分公司-电脑部-电脑1部-精英组                                 4
7           张明华                                                1           总部                                                 0           1           总部                                                 0
8           陈星                                                 2           上海分公司                                              1           2           上海分公司                                              1
9           李春华                                                5           上海分公司-电脑部                                          2           5           上海分公司-电脑部                                          2(9 行受影响)

解决方案 »

  1.   


    大哥,后面的Name和Tree,是需要公司级的,不是部门级的。你没有看清楚我的需求哟。
      

  2.   

    这个难点在于,要通过ParentId逐级往上查,最后查到公司级别。公司级别有两个:总部  和   上海分公司
    其他都是部门。
      

  3.   

    补充一下:表[Compay]里面有一个Type字段,Type=0就是公司极别的,Type=1就是部门级别的。
      

  4.   


    create FUNCTION  getParentID(@id int)
    RETURNS nvarchar(2000) 
    AS
    BEGIN
    DECLARE @result nvarchar(2000)
    set @result='' 
    ;with SubTab
    as
    (
    select [ID],[ParentID]
    from [Compay]
    where [ID]=@id
    union all
    select a.[ID],a.[ParentID]
    from [Compay] a,SubTab b
    where a.[ID]=b.[ParentID]
    )
    select @result=[ID] from SubTab where [ParentID]=0
    RETURN @result
    end
    select U.Id,U.Name,C.Id,C.Name, case when Tree>0 then dbo.getParentID(Tree) else 0 end as tree from [UserName] U 
    inner join [User_Compay] UC on U.Id=UC.UserId 
    inner join  [Compay] C on UC.CompayId=C.IdId          Name                                               Id          Name                                               tree
    ----------- -------------------------------------------------- ----------- -------------------------------------------------- -----------
    1           杜玲利                                                3           总部-人事部                                             1
    2           唐红                                                 6           上海分公司-电脑部-电脑1部                                     1
    3           李小林                                                2           上海分公司                                              1
    4           张光                                                 5           上海分公司-电脑部                                          1
    5           王涛                                                 4           上海分公司-销售部                                          1
    6           贾云                                                 7           上海分公司-电脑部-电脑1部-精英组                                 1
    7           张明华                                                1           总部                                                 0
    8           陈星                                                 2           上海分公司                                              1
    9           李春华                                                5           上海分公司-电脑部                                          1(9 行受影响)
    不好意思,没细看
    这是典型的递归哈,看最后一列的tree可以递归到了,其他的就就不一一写了,
    就是这个意思了,你再动手改改吧,睡觉了,哈哈
      

  5.   

    通过ParentId逐级往上查,这个不难。主要在于你没有区分出 总部  和   上海分公司这两个公司级别。因为上海分公司的上级有事总部。对于select 3, '总部-人事部', 1, 1 union all 这条数据,如果要往上查的话,可以查到总部,但是如果其他数据如果查到上海分公司的时候还要不要继续往上查。
    造成这个的原因,在于你这条数据有点问题,既然是总部的人事部,他的上级单位也就是parientid为啥是上海分公司,这不合理吧。
      

  6.   

    create function f_getCompany
    (@id  int)
    returns @t table([Id] int,[Name] [nvarchar](50),[ParentId] int,[Tree] int,Type int)
    as
    begin ;with
    wang as
    (select * from Compay where id=@id 
    union all
    select compay.* from compay join wang on compay.id=wang.ParentId
    )
    insert into @T
    select top 1 * from wang where type=0 order by ID desc
    return 
    end
    select username.id,username.name,部门id=user_compay.CompayId,部门名称=Compay.Name,Compay.Tree
    ,公司id=s.id,公司名称=s.name,公司tree=s.tree
    from username join user_compay on username.id=user_compay.id
          join Compay on user_compay.CompayId=Compay.Id
          cross apply dbo.f_getCompany(Compay.Tree) s
          
        
    1 杜玲利 3 总部-人事部 1 1 总部 0
    3 李小林 2 上海分公司 1 1 总部 0
    4 张光 4 上海分公司-销售部 2 2 上海分公司 1
    5 王涛 6 上海分公司-电脑部-电脑1部 3 1 总部 0
    6 贾云 5 上海分公司-电脑部 2 2 上海分公司 1
    7 张明华 7 上海分公司-电脑部-电脑1部-精英组 4 2 上海分公司 1
    8 陈星 2 上海分公司 1 1 总部 0
    9 李春华 5 上海分公司-电脑部 2 2 上海分公司 1
      

  7.   

    通过ParentId逐级往上查,这个不难。主要在于你没有区分出 总部  和   上海分公司这两个公司级别。因为上海分公司的上级有事总部。对于select 3, '总部-人事部', 1, 1 union all 这条数据,如果要往上查的话,可以查到总部,但是如果其他数据如果查到上海分公司的时候还要不要继续往上查。
    造成这个的原因,在于你这条数据有点问题,既然是总部的人事部,他的上级单位也就是parientid为啥是上海分公司,这不合理吧。
    总部和上海分公司的Type=0,其他部门级别的Type=1。
    要判断一下,查到上海分公司的时候,就不能往上查了因为上海分公司是一个公司级别的。
    第三条数据没有问题呀。总部的人事部,是属于总部下面的部门呀。上级单位是总部呀。
      

  8.   

    CREATE TABLE [UserName] ([Id] int,[Name] [nvarchar](50))
    insert into [UserName]
    select 1, '杜玲利' union all
    select 2, '唐红' union all
    select 3, '李小林' union all
    select 4, '张光' union all
    select 5, '王涛' union all
    select 6, '贾云' union all
    select 7, '张明华'union all
    select 8, '陈星' union all
    select 9, '李春华'  CREATE TABLE [Compay] ([Id] int,[Name] [nvarchar](50),[ParentId] int,[Tree] int)
    insert into [Compay]
    select 1, '总部', 0, 0 union all
    select 2, '上海分公司', 1, 1 union all
    select 3, '总部-人事部', 1, 1 union all
    select 4, '上海分公司-销售部', 2, 2 union all
    select 5, '上海分公司-电脑部', 2, 2 union all
    select 6, '上海分公司-电脑部-电脑1部', 5, 3 union all
    select 7, '上海分公司-电脑部-电脑1部-精英组', 6, 4 
    CREATE TABLE [User_Compay] ([Id] int,[UserId] int,[CompayId] int)
    insert into [User_Compay]
    select 1, 1, 3 union all
    select 2, 7, 1 union all
    select 3, 3, 2 union all
    select 4, 5, 4 union all
    select 5, 2, 6 union all
    select 6, 4, 5 union all
    select 7, 6, 7 union all
    select 8, 8, 2 union all
    select 9, 9, 5 create function get_up (@id int)
    returns int
    as begin 
    declare @parentid int
    ;with sel as(
     select id,parentid, 1 as lev from compay where id=@id
    union all
    select a.id,a.parentid,b.lev+1  from compay a
    join sel b on a.id=b.parentid  and a.parentid<>0
    )
    select top 1  @parentid=id from sel order by lev desc
    return @parentid
    endselect a.id 员工ID,a.name 员工姓名, b.compayid 部门ID,
    c.name 部门名,c.tree 部门Tree,dbo.get_up(b.compayid) as 公司ID,公司名=(select name from compay where id=
    dbo.get_up(b.compayid)),公司Tree=(select tree from compay where id=dbo.get_up(b.compayid)) 
    from [userName] a
     join [User_Compay] b on a.id=b.userid
     join compay c on b.compayid=c.id
      

  9.   

    tangguangqiang,你这个数据查出来有点不对。杜玲利的部门是:总部-人事部  对应的公司名应该是总部.....
    公司的Id也不对。
      

  10.   

    再请教一下,我把get_up (@id int),这个id传进去一个字符串,但字符串以数字开头的话就要报错。
    消息 245,级别 16,状态 1,第 1 行
    在将 nvarchar 值 'BEF83D73-72D1-4FD2-8532-6BC25015F00A' 转换成数据类型 int 时失败。
    怎么改一下getup那个函数能传进去字符。
    我已经改成这样了。还是不行。ALTER FUNCTION [dbo].[get_up] (@id nvarchar(36))
    RETURNS nvarchar(max)
    as
    BEGIN 
    declare @parentid nvarchar(4000)
    ;with sel as(
    select id,parentid, 1 as lev from tb_Organization where id=@id
    union all
    select a.id,a.parentid,b.lev+1  from tb_Organization a
    join sel b on a.id=b.parentid  and a.parentid<>0
    )
    select top 1  @parentid=id from sel order by lev desc
    return convert(nvarchar(36),@parentid) 
    END
      

  11.   


    create function get_up (@id int)
    returns int
    as begin 
    DECLARE @count  INT ,@idd INT ,@atype INT ,@tree INT 
    SET @count=(SELECT COUNT(*) FROM dbo.Compay)
    SET @atype=1 
    WHILE (@count>0)
    BEGIN 
    IF @atype=1
    BEGIN 
    SET @idd=@id 
    SELECT @id=tree ,@atype=atype FROM dbo.Compay WHERE id=@id 
    END
    ELSE 
    BREAK 
    SET @count=@count-1
    END 
    return @idd
    END
    go
    select a.id 员工ID,a.name 员工姓名, b.compayid 部门ID,
    c.name 部门名,c.tree 部门Tree,dbo.get_up(b.compayid) as 公司ID,公司名=(select name from compay where id=
    dbo.get_up(b.compayid)),公司Tree=(select tree from compay where id=dbo.get_up(b.compayid)) 
    from [userName] a
     join [User_Compay] b on a.id=b.userid
     join compay c on b.compayid=c.id  ORDER BY  a.id
      

  12.   


    SELECT @id=ParentId ,@atype=atype FROM dbo.Compay WHERE id=@id 
    或者
    SELECT @id=tree ,@atype=atype FROM dbo.Compay WHERE id=@id