例如,有一张表是父子管理,TH表示父图号,zth表示子图号
th zth
a   b
a   f
b   c
b   d
c   e
x   g
g   i
g   h
h   d
我想知道如何可以通过d查到树根是a和x

解决方案 »

  1.   

    创建用户定义函数,每个子节点de父节点的信息
    --生成测试数据
    create table BOM(ID int,parentID int,sClassName varchar(10))
    insert into BOM values(1,0,'1111'      )
    insert into BOM values(2,1,'1111_1'    )
    insert into BOM values(3,2,'1111-1-1'  )
    insert into BOM values(4,3,'1111-1-1-1') 
    insert into BOM values(5,1,'1111-2'    )go--创建用户定义函数,每个子节点de父节点的信息
    create function f_getParent(@ID int)
    returns varchar(40)
    as
    begin
        declare @ret varchar(40)    while exists(select 1 from BOM where ID=@ID and parentID<>0)
        begin
            select @ID=b.ID,@ret=','+rtrim(b.ID)+isnull(@ret,'')
            from
                BOM a,BOM b
            where
                a.ID=@ID and b.ID=a.parentID
        end
        
        set @ret=stuff(@ret,1,1,'')
        return @ret
    end
    go--执行查询
    select ID,isnull(dbo.f_getParent(ID),'') as parentID from BOM
    go--输出结果
    /*
    ID          parentID                                 
    ----------- ---------------------------------------- 
    1           
    2           1
    3           1,2
    4           1,2,3
    5           1   
    */--删除测试数据
    drop function f_getParent
    drop table BOM
    go
      

  2.   

    /*
    标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(字符串形式显示)
    作者:爱新觉罗?毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2010-02-02
    地点:新疆乌鲁木齐
    */--生成测试数据 
    create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--创建用户定义函数 
    create function f_cid(@id varchar(10)) returns varchar(8000) 
    as 
    begin 
      declare @i int , @ret varchar(8000) 
      declare @t table(id varchar(10) , pid varchar(10) , level int) 
      set @i = 1 
      insert into @t select id , pid , @i from tb where id = @id 
      while @@rowcount <> 0 
      begin 
        set @i = @i + 1 
        insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
      end 
      select @ret = isnull(@ret , '') + id + ',' from @t 
      return left(@ret , len(@ret) - 1)
    end 
    go --执行查询 
    select id , children = isnull(dbo.f_cid(id) , '') from tb group by iddrop table tb
    drop function f_cid/*
    id   children                               
    ---- ---------------------------------------
    001  001,002,003,004,005,006,007,008,009,010
    002  002,004
    003  003,005,006,007,008,009,010
    004  004
    005  005
    006  006
    007  007,008,009,010
    008  008
    009  009
    010  010(所影响的行数为 10 行)
    *//*
    标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(字符串形式显示)
    作者:爱新觉罗?毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2010-02-02
    地点:新疆乌鲁木齐
    */create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
    insert into tb values('001' , null  , N'广东省')
    insert into tb values('002' , '001' , N'广州市')
    insert into tb values('003' , '001' , N'深圳市')
    insert into tb values('004' , '002' , N'天河区')
    insert into tb values('005' , '003' , N'罗湖区')
    insert into tb values('006' , '003' , N'福田区')
    insert into tb values('007' , '003' , N'宝安区')
    insert into tb values('008' , '007' , N'西乡镇')
    insert into tb values('009' , '007' , N'龙华镇')
    insert into tb values('010' , '007' , N'松岗镇')
    go;with t as
    (
        select id , cid = id from tb 
        union all
        select t.id , cid = tb.id 
        from t join tb on tb.pid = t.cid 
    )
    select id , cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
    from tb
    group by id
    order by id
    /*
    id   cid
    ---- ---------------------------------------
    001  001,002,003,005,006,007,008,009,010,004
    002  002,004
    003  003,005,006,007,008,009,010
    004  004
    005  005
    006  006
    007  007,008,009,010
    008  008
    009  009
    010  010(10 行受影响)
    */;with t as
    (
        select id , name , cid = id , path = cast(name as nvarchar(100)) from tb 
        union all
        select t.id , t.name , cid = tb.id , path = cast(tb.name as nvarchar(100))
        from t join tb on tb.pid = t.cid 
    )
    select id , name , 
           cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , ''),
           path = STUFF((SELECT ',' + path FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
    from tb
    group by id , name
    order by id
    /*
    id   name       cid                                         path
    ---- ---------- ------------------------------------------- ---------------------------------------------------------------------
    001  广东省     001,002,003,005,006,007,008,009,010,004     广东省,广州市,深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇,天河区
    002  广州市     002,004                                     广州市,天河区
    003  深圳市     003,005,006,007,008,009,010                 深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇
    004  天河区     004                                         天河区
    005  罗湖区     005                                         罗湖区
    006  福田区     006                                         福田区
    007  宝安区     007,008,009,010                             宝安区,西乡镇,龙华镇,松岗镇
    008  西乡镇     008                                         西乡镇
    009  龙华镇     009                                         龙华镇
    010  松岗镇     010                                         松岗镇(10 行受影响)
    */drop table tb
      

  3.   

    ------------------------------------------------------------------------
    -- Author : HappyFlyStone  
    -- Date   : 2010-10-07 
    -- Version: Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86) 
    --       Mar 29 2009 10:27:29 
    --       Copyright (c) 1988-2008 Microsoft Corporation
    --       Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)
    --      
    -------------------------------------------------------------------------- Test Data: ta
    IF OBJECT_ID('[ta]') IS NOT NULL 
        DROP TABLE [ta]
    Go
    CREATE TABLE ta([th] NVARCHAR(1),[zth] NVARCHAR(1))
    Go
    INSERT INTO ta
        SELECT 'a','b' UNION ALL
        SELECT 'a','f' UNION ALL
        SELECT 'b','c' UNION ALL
        SELECT 'b','d' UNION ALL
        SELECT 'c','e' UNION ALL
        SELECT 'x','g' UNION ALL
        SELECT 'g','i' UNION ALL
        SELECT 'g','h' UNION ALL
        SELECT 'h','d' 
    GO
    --Start
    with cte
    AS
    (
    select 1 as rid,* from ta where zth = 'd'
    union all
    select case when exists(select 1 from ta where zth = a.th) then 1 else 0 end, a.* from ta a,cte b where a.zth = b.th
    )
    select th,zth from cte
    where rid = 0
    --Result:
    /*
    th   zth
    ---- ----
    x    g
    a    b(2 行受影响)*/
    --End 
      

  4.   

    create table #EnterPrise
    (
      Department nvarchar(50),--部门名称
      ParentDept nvarchar(50),--上级部门
      DepartManage nvarchar(30)--部门经理
    )
    insert into #EnterPrise select '技术部','总经办','Tom'
    insert into #EnterPrise select '商务部','总经办','Jeffry'
    insert into #EnterPrise select '商务一部','商务部','ViVi'
    insert into #EnterPrise select '商务二部','商务部','Peter'
    insert into #EnterPrise select '程序组','技术部','GiGi'
    insert into #EnterPrise select '设计组','技术部','yoyo'
    insert into #EnterPrise select '专项组','程序组','Yue'
    insert into #EnterPrise select '总经办','','Boss'
    --查询部门经理是Tom的下面的部门名称
    ;with hgo as
    (
       select *,0 as rank from #EnterPrise where DepartManage='Tom'
       union all
       select h.*,h1.rank+1 from #EnterPrise h join hgo h1 on h.ParentDept=h1.Department
    )
    select * from hgo
    /*
    Department           ParentDept                DepartManage      rank
    --------------- -------------------- ----------------------- -----------
    技术部               总经办                    Tom               0
    程序组               技术部                    GiGi              1
    设计组               技术部                    yoyo              1
    专项组               程序组                    Yue               2
    */
    --查询部门经理是GiGi的上级部门名称
    ;with hgo as
    (
       select *,0 as rank from #EnterPrise where DepartManage='GiGi'
       union all
       select h.*,h1.rank+1 from #EnterPrise h join hgo h1 on h.Department=h1.ParentDept
    )
    select * from hgo
    /*
    Department               ParentDept          DepartManage    rank
    -------------------- ----------------------  -----------  -----------
    程序组                   技术部                 GiGi           0
    技术部                   总经办                 Tom            1
    总经办                                          Boss           2
    */本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2010/01/31/5274571.aspx
      

  5.   

    --生成测试数据
    create table BOM(ID int,parentID int,sClassName varchar(10))
    insert into BOM values(1,0,'1111'      )
    insert into BOM values(2,1,'1111_1'    )
    insert into BOM values(3,2,'1111-1-1'  )
    insert into BOM values(4,3,'1111-1-1-1') 
    insert into BOM values(5,1,'1111-2'    )declare @i nvarchar(50);
    with tb as(
    select * from bom where id=4
    union all
    select a.* from bom a,tb b where a.id=b.parentID
    )
    select @i=(select ','+ltrim(str(id)) from tb  order by id for xml path(''))
    print stuff(@i,1,1,'')
    /*结果*/
    1,2,3,4