返回什么?一个ID可以多个CHILDID?假设1个ID最多只有一个childid,大致思路可否这样:declare @fatherid int,@childid int
select @fatherid = XXX ,@childid = 0
while (@fatherid is not bull)
    begin
        select @childid=childid from table where id = @fatherid
        select @fatherid = @childid
   end
return 0

解决方案 »

  1.   

    declare @fatherid int,@childid int
    select @fatherid = XXX 
    while (@fatherid is not null)
    begin
    select @childid=childid from table where id = @fatherid
    select @fatherid = @childid
    end
    return 0
      

  2.   

    CREATE PRO GetChildID(@InID int)
    as
    DECLARE @TempChidlID int 
    select @TempChildID = ChildID from Table where ID = @InID
    if str(@TempChildID) = ''
    begin
    return
    end
    else
    exec gechildID(@tempChildID)
      

  3.   

    请楼主参考一下这个帖子,关于树形数据的:
    http://community.csdn.net/Expert/topic/5035/5035486.xml?temp=.6427881
      

  4.   

    怪我说简单了!这其实是一个2叉数结构,我先想求一个点下面有多少个点!
    它不只有一个ChildID!它有两个:ChildIDL,ChildIDR!我程序的逻辑是这样的:
    dim iNum
    function getNum(byval ID)
      sql="select ChildIDL,ChildIDR from table where ID=" & ID
      rs.open sql,conn
      if rs(0)<>-1 then     '表示如果有值
        getNum(rs(0))
      endif
      if rs(1)<>-1 then
        getNum(rs(1))
      endif
    end function
    就是这么个过程,只要这个过程实现了,我可以自己再在其中加我想要的东西!
      

  5.   

    to:hellowork(一两清风)
    网页看到了,挺像我需要的问题,我回去看看能不能解决我的所有问题!
    先谢谢!看完后结贴
      

  6.   

    create function CheckZX(@FID varchar(20) , @CID varchar(20))
    returns bit
    as
    begin
      declare @R bit
      declare @Loop bit
      declare @ID varchar(20)
      set @R=0
      set @Loop=0
      
      while @Loop=0
      begin
        select
          @ID=ID
        from
          BOM
        where
          pid=@CID
        
        if @ID=@FID
        begin
          set @Loop=1
          set @R=1
        end
        else
        begin
          if @ID='a'
          begin
            set @Loop=1
            set @R=0
          end
          else
          begin
            set @CID=@ID
          end
        end
      end
      return @R
    end我想判断@CID是不是@FID的体系中!如果在就返回“1”不再返回“0”,我对自定义函数不太明白,这个无法查出!希望大家给与帮助!
      

  7.   

    create function CheckZX(@FID varchar(20) , @CID varchar(20))
    returns bit
    as
    begin
      declare @R bit
      declare @Loop bit
      declare @ID varchar(20)
      set @R=0
      set @Loop=0
      
      while @Loop=0
      begin
        select
          @ID=ID
        from
          BOM
        where
          pid=@CID
        
        if @ID=@FID
        begin
          set @Loop=1
          set @R=1
        end
        else
        begin
          if @ID='a'
          begin
            set @Loop=1
            set @R=0
          end
          else
          begin
            set @CID=@ID
          end
        end
      end
      return @R
    end我想判断@CID是不是@FID的体系中!如果在就返回“1”不再返回“0”,我对自定义函数不太明白,这个无法查出!希望大家给与帮助!请会的再帮我看看呀!谢谢了