create procedure p_aaaa 
as 
select distinct convert(char(18),id)  from Client

解决方案 »

  1.   


    create table  Client(id int ,parentid int)
    insert into Client select 1,null
    insert into Client select 2 ,                     1
    insert into Client select 3  ,                    1
    insert into Client select 4   ,                   2
    insert into Client select 5    ,                  1
    go
    create proc proc_out
    @str varchar(8000) output
    as
    select @str=''
    select @str=@str+convert(varchar(100),id)+','  from client
    select @str=left(@str,len(@str)-1)
    godeclare @strout varchar(8000)
    exec proc_out @strout output
    print @strout
      

  2.   

    为叶子结点列出相邻指定级数(三级)祖先select id,parentid
    ,(select parentid 
              from T 
             where id = a.parentid) 
    ,(select parentid 
              from T 
             where id = (select parentid 
                           from T 
                          where id = a.parentid)) 
    ,(select parentid 
              from T 
             where id = ((select parentid 
                            from T 
                           where id = (select parentid 
                                         from T 
                                        where id = a.parentid)))) 
    from T a
    where id not in (select parentid from tree)
      

  3.   

    http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.1570551树形数据处理方案
      

  4.   

    拜托,各位老大,要是这么简单我就不用请教各位了,不过还是谢谢!
    抛砖引玉:
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[up_GetChildInfoString]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[up_GetChildInfoString]
    GO
    CREATE PROCEDURE up_GetChildInfoString

    @m_objDB varchar(50),@m_sParentField varchar(100),@m_sParentValue numeric,@m_ReturnValue varchar(1000) output
    As Declare @objDB varchar(50)
    Set @objDB=@m_objDBDeclare @id_Temp numericDeclare  Cur_Info scroll  Cursor for  Select  id  from business..project where ParentPrj=@m_sParentValue
    open   Cur_Info
    Fetch Next from Cur_Info into @id_Temp
    while(@@Fetch_Status=0) begin

    set @m_ReturnValue=@m_ReturnValue+';'+cast(@id_Temp as varchar(1000))
    print @m_ReturnValue 
    Fetch Next from Cur_Info into @id_Temp
    end
    close  Cur_Info
    Deallocate Cur_Info这只能查出一级的,在储过程中怎么才能查多级的,我在存储过程中不会用递归
      

  5.   

    declare @ varchar(8000)
    set @=''
    select @=@+','+cast(id as varchar(10)) from clientselect right(@,len(@)-1) 结果
      

  6.   

    参考下面的函数,id为编号,pid为上级编号:/*-- 得到指定id的子id列表 --*/
    --不包含排序字段的情况
    create function f_getchildid(@id int)
    returns @re table(id int)
    as
    begin
    insert into @re select id from tb where pid=@id
    while @@rowcount>0
    insert into @re select a.id 
    from tb a inner join @re b on a.pid=b.id
    where a.id not in(select id from @re)
    return
    end
    go--包含排序字段的情况
    create function f_getchildidsort(@id int)
    returns @re table(id int,sortid varchar(8000))
    as
    begin
    --为了数字排序正常,需要统一编码宽度
    declare @idlen int,@idheader varchar(20)
    select @idlen=max(len(id))
    ,@idheader=space(@idlen)
    from tb insert into @re select id,right(@idheader+cast(id as varchar),@idlen)
    from tb where pid=@id
    while @@rowcount>0
    insert into @re select a.id,right(@idheader+cast(a.id as varchar),@idlen)+','+b.sortid 
    from tb a inner join @re b on a.pid=b.id
    where a.id not in(select id from @re)
    return
    end
    go--调用示例,显示1的所有子.
    select a.* from tb a inner join dbo.f_getchildidsort(1) b on a.id=b.id order by b.sortid
      

  7.   

    注意一点,在我的处理方法中,如果某个id为顶级,则父id的值为0,而不是null.
    函数返回的是表集.
      

  8.   

    楼上的,你是不是内联的啊?我已前是写在com中的用递归的方法,结果发现在sqlServer7.0中用select where id in(得到的字符串),有时能查出,有时查不出,可能是7.0的缺陷,2000中就没有,我才想写到存储过程中,哈哈
      

  9.   

    楼上的,你是不是内联的啊?我已前是写在com中的用递归的方法,结果发现在sqlServer7.0中用select where id in(得到的字符串),有时能查出,有时查不出,可能是7.0的缺陷,2000中就没有,我才想写到存储过程中,哈哈
      

  10.   

    如果楼主要将其所有某id的所有子id生成一个字符串,可以用:
    declare @str varchar(8000)
    set @str=''
    select @str=@str+','+cast(id as varchar0 from dbo.f_getchildid(1)
    set @str=substring(@str,2,8000)
    print @str
    更多的内容参考:
    http://expert.csdn.net/Expert/topic/2285/2285830.xml?temp=.1570551
      

  11.   

    一道 SQL 题 ... (关于树型结构的在关系表中的存储及其应用处理)
    http://www.csdn.net/Develop/Read_Article.asp?Id=17247
      

  12.   

    --为叶子结点列出相邻指定级数(四级)祖先
    select id,parentid
    ,(select parentid 
        from Tree 
       where id = a.parentid) 
    ,(select parentid 
        from Tree 
       where id = (select parentid 
                     from Tree 
                    where id = a.parentid )) 
    ,(select parentid 
        from Tree 
       where id = ((select parentid 
                      from Tree 
                     where id = (select parentid 
                                   from Tree 
                                  where id = a.parentid )))) 
    ,(select parentid 
        from Tree 
       where id =( (select parentid 
        from Tree 
       where id = ((select parentid 
                      from Tree 
                     where id = (select parentid 
                                   from Tree 
                                  where id = a.parentid ))))   ))from Tree a
    where id not in (select parentid from tree)一道 SQL 题 ... (关于树型结构的在关系表中的存储及其应用处理)
    http://www.csdn.net/Develop/Read_Article.asp?Id=17247树型结构数据在数据库基本表中的存储及维护  
    http://www.csdn.net/Develop/Read_Article.asp?Id=18666