declare @tenterprise_userid varchar(10),@siteid varchar(10)
set @tenterprise_userid=$userid$
set @siteid=$siteid$;
--chenji ÓÃÓÚ·ÀÖ¹ÎÞÏÞµÝ¹é
with tenterprise_users#(siteid, tenterprise_userid,chenji) as (
    select siteid, tenterprise_userid,1 as chenji
    from tenterprise_users
    where siteid = @siteid
      and tenterprise_userid = @tenterprise_userid
    union all
    select t1.siteid, t1.fsubid,t2.chenji+1
    from tenterprise_userdetail t1
             inner join tenterprise_users# t2 on t1.siteid = t2.siteid and t2.tenterprise_userid = t1.fparentid
    where t2.chenji<10
)
select  distinct t1.siteid, t1.tenterprise_userid, t2.fname, t2.fphonenumber
from tenterprise_users# t1
         inner join tenterprise_users t2 on t1.siteid = t2.siteid and t1.tenterprise_userid = t2.tenterprise_userid

