create proc test
@bh varchar(10)
as
set nocount on
declare @i int
declare @t table(Parent_id varcahr(20),item_id  varcahr(20),iv int)
declare @t1 table(Parent_id varcahr(20),item_id  varcahr(20))
insert into @t1
select 会员编号,下级会员编号  from  table1  where 会员编号=@bh
set @i=1
while  exists(select TOP 1 * from @t1)
begin
 insert into @t(Parent_id ,item_id ,iv )
 select 会员编号,下级会员编号,@i from @t1
 delete from  @t1
  insert into @t1    
  select 会员编号,下级会员编号 from  table where 会员编号 in (select item_id  from @t where iv=@i)
  if @@rowcount=0
     break
  set @i=@i+1
end
select count(*),max(iv) from @tset nocount off