有一个表
ID     ParentId
------------------------------------
1         -1
2         1
3         1
4         2
5         2
6         3
7         3
8         7
9         7
10        7这个表的数据表示了一个树形结构
           |--4
    |--2---|
1---|      |--5
    |
    |      |--6
    |--3---|
           |     |--8
           |--7--|--9
                 |--10现在的问题是这样的,我把树形里选中的叶子结点,以字符串的形式传给存储过程
在存储过程里实现以下的功能:
如果我选中的是8,9,10,那么最终需要的结点是7,因为7所有的子节点都被选中了
如果我选中的是4,5,8,9,那么最终需要的结点是2,8,9,因为2的子节点都被选中了,但是7的子节点中10未被选中,所以只能单独存储8,9我现在就需要在一个存储过程里处理传进来的已经选择的末级子节点,按照上面的规则生成出最终的结点数据
请问该怎么处理,谢谢

解决方案 »

  1.   

    你这个需求难办,如果只是查询各种子节点,父节点到好办,参考如下内容:/*
    标题: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 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_pid(@id varchar(3)) returns varchar(100)
    as
    begin
      declare @re_str as varchar(100)
      set @re_str = ''
      select @re_str = name from tb where id = @id
      while exists (select 1 from tb where id = @id and pid is not null)
        begin
          select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
        end
      return @re_str
    end
    goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
    drop function f_pid/*
    id   pid  name       路径                       
    ---- ---- ---------- ---------------------------
    001  NULL 广东省     广东省
    002  001  广州市     广东省,广州市
    003  001  深圳市     广东省,深圳市
    004  002  天河区     广东省,广州市,天河区
    005  003  罗湖区     广东省,深圳市,罗湖区
    006  003  福田区     广东省,深圳市,福田区
    007  003  宝安区     广东省,深圳市,宝安区
    008  007  西乡镇     广东省,深圳市,宝安区,西乡镇
    009  007  龙华镇     广东省,深圳市,宝安区,龙华镇
    010  007  松岗镇     广东省,深圳市,宝安区,松岗镇(所影响的行数为 10 行)
    */
      

  2.   


    /*
    标题: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
    /*
    标题: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 , pid = id from tb 
        union all
        select t.id , pid = tb.pid 
        from t join tb on tb.id = t.pid 
    )
    select id , pid = STUFF((SELECT ',' + pid FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
    from tb
    group by id
    order by id
    /*
    id   pid
    ---- -------------
    001  001
    002  002,001
    003  003,001
    004  004,002,001
    005  005,003,001
    006  006,003,001
    007  007,003,001
    008  008,007,003,001
    009  009,007,003,001
    010  010,007,003,001(10 行受影响)
    */;with t as
    (
        select id , name , pid = id , path = cast(name as nvarchar(100)) from tb 
        union all
        select t.id , t.name , pid = tb.pid , path = cast(tb.name as nvarchar(100))
        from t join tb on tb.id = t.pid 
    )
    select id , name ,
           pid = STUFF((SELECT ',' + pid 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       pid               path
    ---- ---------- ----------------- -------------------------------
    001  广东省      001               广东省,广东省
    002  广州市      002,001           广州市,广州市,广东省
    003  深圳市      003,001           深圳市,深圳市,广东省
    004  天河区      004,002,001       天河区,天河区,广州市,广东省
    005  罗湖区      005,003,001       罗湖区,罗湖区,深圳市,广东省
    006  福田区      006,003,001       福田区,福田区,深圳市,广东省
    007  宝安区      007,003,001       宝安区,宝安区,深圳市,广东省
    008  西乡镇      008,007,003,001   西乡镇,西乡镇,宝安区,深圳市,广东省
    009  龙华镇      009,007,003,001   龙华镇,龙华镇,宝安区,深圳市,广东省
    010  松岗镇      010,007,003,001   松岗镇,松岗镇,宝安区,深圳市,广东省(10 行受影响)
    */;with t as
    (
        select id , name , pid = id , path = cast(name as nvarchar(100)) from tb 
        union all
        select t.id , t.name , pid = tb.pid , path = cast(tb.name as nvarchar(100))
        from t join tb on tb.id = t.pid 
    )
    --不知道为什么上面的path中多一个name?
    select id , name , pid , path = substring(path , charindex(',' , path) + 1 , len(path)) from
    (
    select id , name ,
           pid = STUFF((SELECT ',' + pid 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
    ) m
    order by id
    /*
    id   name       pid               path
    ---- ---------- ----------------- -------------------------------
    001  广东省      001               广东省
    002  广州市      002,001           广州市,广东省
    003  深圳市      003,001           深圳市,广东省
    004  天河区      004,002,001       天河区,广州市,广东省
    005  罗湖区      005,003,001       罗湖区,深圳市,广东省
    006  福田区      006,003,001       福田区,深圳市,广东省
    007  宝安区      007,003,001       宝安区,深圳市,广东省
    008  西乡镇      008,007,003,001   西乡镇,宝安区,深圳市,广东省
    009  龙华镇      009,007,003,001   龙华镇,宝安区,深圳市,广东省
    010  松岗镇      010,007,003,001   松岗镇,宝安区,深圳市,广东省(10 行受影响)
    */drop table tb
      

  3.   

    参考楼上尝试了一下,基本思想是将所有含子节点的信息包含在视图中从顶端进行排序,然后,将节点的id按照顺序排序,经过一次遍历便可以得到结果,代码很乱,自己使用可能需要对输入输出形式等进行调整,供参考~/*================================
    Author:sql_lover
    Date:2010-10-25 10:26:29.953
    Function: Find the all_nodes nodes by given nodes 
    =================================*//*Create Tables*/
    if (OBJECT_ID('tree') is not null)
    drop table hotel_count
    create table tree(id int,parentid int)insert into tree
    select 1, -1 union all
    select 2, 1 union all
    select 3, 1 union all
    select 4, 2 union all
    select 5, 2 union all
    select 6, 3 union all
    select 7, 3 union all
    select 8, 7 union all
    select 9, 7 union all
    select 10, 7
    Go/*Create Function Reference up,The Function to achieve all the children*/
    alter 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 , parentid , @i from tree where id = @id 
      while @@rowcount <> 0 
      begin 
        set @i = @i + 1 
        insert into @t select a.id , a.parentid , @i from tree a , @t b where a.parentid = b.id and b.level = @i - 1
      end 
      select @ret = isnull(@ret , '') + id + ',' from @t where id<>@id 
      return left(@ret , len(@ret) - 1)
    end 
    Go /*Create View for Procedure*/
    create view view_chiledren as
    select id , isnull(dbo.f_cid(id) , '') children from tree group by id
    Go/*Create The Main Proc*/
    Create proc fetch_contain_nodes @enodes varchar(1000)
    as
    begin
    declare @str varchar(1000)
    set @str=''
    create table #1(id int)while(CHARINDEX(',',@enodes)>0)
    begin
    insert into #1 select cast(substring(@enodes,1,CHARINDEX(',',@enodes)-1) as int)
    select @enodes=substring(@enodes,CHARINDEX(',',@enodes)+1,len(@enodes))
    end
    insert into #1 select cast(@enodes as int)
    select @str=@str+str(id)+',' from #1 order by id
    select @str=replace(LEFT(@str,LEN(@str)-1),' ','')
    delete from #1
    declare @newstr varchar(1000)
    set @newstr=''
    declare @count int
    declare @id int
    declare @children varchar(1000)
    set @children=''create table #2(id int,children varchar(1000))
    insert into #2 select id,children from view_chiledren where children<>'' and charindex(children,@str)>0select @count=COUNT(1) from #2
    while(@count>0)
    begin
    select top 1 @id=id,@children=children from #2
    select @newstr=@newstr+str(@id)+','
    select @str=replace(@str,@children,'')
    delete from #2 where id=@id
    select @count=@count-1
    enddrop table #2
    declare @result varchar(1000)
    select  @result=@newstr+@str
    while(CHARINDEX(',', @result)>0)
    begin
    insert into #1 select cast(substring(@result,1,CHARINDEX(',', @result)-1) as int)
    select @result=substring(@result,CHARINDEX(',',@result)+1,len(@result))
    end
    insert into #1 select CAST(@result as int)
    delete from #1 where id =''
    select distinct id as allnodes from #1
    drop table #1
    end
    Go/*Test*/
    exec fetch_contain_nodes '1,2,3,4,5,8,9'
    Go/*Result*/
    /*================================
    allnodes
    -----------
    1
    2
    3
    8
    9
    =================================*/
      

  4.   

    create table ta(id varchar(10),parentid varchar(10))
    insert into ta values('1','-1')
    insert into ta values('2','1')
    insert into ta values('3','1')
    insert into ta values('4','2')
    insert into ta values('5','2')
    insert into ta values('6','3')
    insert into ta values('7','4')
    insert into ta values('8','7')
    insert into ta values('9','7')
    insert into ta values('10','7')
    -- 情况1
    --;
    --with ctea as(
    --select parentid,COUNT(id) idcount
    --from ta where parentid in (--select parentid from ta where id in (8,9,10)
    --) group by parentid
    --),
    --cteb as(
    --select parentid,COUNT(id) idcount from ta where id in (8,9,10)  group by parentid
    --)
    --select cteb.parentid id
    --from ctea inner join cteb on ctea.parentid=cteb.parentid and ctea.idcount=cteb.idcount
    --union
    --select ta.id
    --from ctea inner join cteb on ctea.parentid=cteb.parentid and ctea.idcount<>cteb.idcount
    --inner join ta on ta.parentid=ctea.parentid and ta.id in (8,9,10)
    ;
    -- 情况2
    with ctea as(
    select parentid,COUNT(id) idcount
    from ta where parentid in (select parentid from ta where id in (8,9,4,5)
    ) group by parentid
    ),
    cteb as(
    select parentid,COUNT(id) idcount from ta where id in (8,9,4,5)  group by parentid
    )
    select cteb.parentid id
    from ctea inner join cteb on ctea.parentid=cteb.parentid and ctea.idcount=cteb.idcount
    union
    select ta.id
    from ctea inner join cteb on ctea.parentid=cteb.parentid and ctea.idcount<>cteb.idcount
    inner join ta on ta.parentid=ctea.parentid and ta.id in (8,9,4,5)drop table ta /**
    ID   
    ---
    2
    8
    9(3 行受影响)
    **/
      

  5.   

    --建表,插入演示数据
    create table tree(id int, parentid int)
    insert into tree
    select 1, -1 union all
    select 2, 1 union all
    select 3, 1 union all
    select 4, 2 union all
    select 5, 2 union all
    select 6, 3 union all
    select 7, 3 union all
    select 8, 7 union all
    select 9, 7 union all
    select 10, 7--建函数
    if object_id('getnode','TF') is not null
    drop function getnode
    gocreate function getnode(@s varchar(1000))
    returns @tb table(id int)
    as
    begin
    declare @temptb table(id int, parentid int) insert @tb
        select SUBSTRING(@s,number,CHARINDEX(',',@s+',',number+1)-number)
        from master..spt_values
        where CHARINDEX(',',','+@s,number) = number AND type = 'P' while @@rowcount <> 0
    begin
    delete @temptb ;with t1 as
    (
    select a.id,a.parentid,b.id as bid 
    from tree a left join (
    select a.id,b.parentid from @tb a join tree b on a.id = b.id
    ) b on a.parentid = b.parentid and a.id = b.id
    )

    insert into @temptb
    select id,parentid from t1 a where not exists(select 1 from t1 where parentid = a.parentid and bid is null) delete @tb where id in (select id from @temptb)
    insert @tb select distinct parentid from @temptb
    end return
    end--执行
    select * from getnode('4,5,8,9')
    /*
    id
    -----------
    2
    8
    9(3 行受影响)
    */select * from getnode('6,8,9,10')
    /*
    id
    -----------
    3(1 行受影响)
    */
      

  6.   

    刚刚忘了框起来了....--建表,插入演示数据
    create table tree(id int, parentid int)
    insert into tree
    select 1, -1 union all
    select 2, 1 union all
    select 3, 1 union all
    select 4, 2 union all
    select 5, 2 union all
    select 6, 3 union all
    select 7, 3 union all
    select 8, 7 union all
    select 9, 7 union all
    select 10, 7--建函数
    if object_id('getnode','TF') is not null
    drop function getnode
    gocreate function getnode(@s varchar(1000))
    returns @tb table(id int)
    as
    begin
    declare @temptb table(id int, parentid int)insert @tb
      select SUBSTRING(@s,number,CHARINDEX(',',@s+',',number+1)-number)
      from master..spt_values
      where CHARINDEX(',',','+@s,number) = number AND type = 'P'while @@rowcount <> 0
    begin
    delete @temptb ;with t1 as
    (
    select a.id,a.parentid,b.id as bid  
    from tree a left join (
    select a.id,b.parentid from @tb a join tree b on a.id = b.id
    ) b on a.parentid = b.parentid and a.id = b.id
    )insert into @temptb
    select id,parentid from t1 a where not exists(select 1 from t1 where parentid = a.parentid and bid is null)delete @tb where id in (select id from @temptb)
    insert @tb select distinct parentid from @temptb
    endreturn
    end--执行
    select * from getnode('4,5,8,9')
    /*
    id
    -----------
    2
    8
    9(3 行受影响)
    */select * from getnode('6,8,9,10')
    /*
    id
    -----------
    3(1 行受影响)
    */