ah, I see. How about this?--------------------------------------DDL, DMLset nocount ondeclare @UNITCONVERsION table (IDfrom int ,FromValue int ,toValue int ,IDto int)insert @UNITCONVERsIONselect 2,1000,1,3 unionselect 3,1000,1,4 unionselect 4,1000,1,8 unionselect 33,1000,1,38 unionselect 103,2,1,205 unionselect 56,1000,1,33 unionselect 38,10,1,45 unionselect 205,10,1,506 unionselect 45,8,1,103------------------------------------declare @parm intset @parm = 38declare @tb table (Lev int ,IDfrom int ,FromValue int ,toValue int ,IDto int)declare @lev int ,@IDto intset @lev = 0--insert Starting recordinsert @tbselect top 1 @lev ,IDFrom ,FromValue ,toValue ,IDtofrom @UNITCONVERsIONwhere IDto = @parmor IDfrom = @parm--work through backwards from starting poswhile @@rowcount > 0begin select @lev = @lev - 1 insert @tb select @lev ,a.IDFrom ,a.FromValue ,a.toValue ,a.IDto from @UNITCONVERsION a join @tb b on a.IDto = b.IDfrom and b.lev = @lev + 1end--reset level to starting posselect @lev = 0--work through forwards from starting poswhile @@rowcount > 0begin select @lev = @lev + 1 insert @tb select @lev ,a.IDFrom ,a.FromValue ,a.toValue ,a.IDto from @UNITCONVERsION a join @tb b on a.IDfrom = b.IDto and b.lev = @lev - 1endselect * from @tb order by lev
Be One with the OptimizerTG