with cte as ( select belongsagent from [qpproxydb].[dbo].[bs_proxyinfo] where proxyid = @proxyid union all select a.proxyid from [qpproxydb].[dbo].[bs_proxyinfo] a join cte b on a.proxyid = b.belongsagent ) select * from cte order by belongsagent asc