@MAXLEVELS int 
AS 
BEGIN
	   SET XACT_ABORT ON
	   SET NOCOUNT ON;	   

	   DECLARE @LEVEL int
	   SET @LEVEL = 1

	   SET @MAXLEVELS = COALESCE(@MAXLEVELS, 100)
	   IF @MAXLEVELS > 1000 Set @MAXLEVELS = 1000

	   CREATE TABLE #AAutoDiscovered (
		   src int,
		   tgt int,
		   ord int
		   )
	
	   INSERT INTO #AAutoDiscovered (src,tgt,ord) 
	   SELECT #numeric_param_temp.*, null, 0 FROM  #numeric_param_temp;


	   WHILE @@ROWCOUNT > 0 and @MAXLEVELS >= @LEVEL
	   BEGIN
		   Set @LEVEL = @LEVEL + 1
		   insert into #AAutoDiscovered (src, tgt, ord)
		   select e.PreceedingNetworkID, e.NetworkID, min(d.ord) + 1
			   from #AAutoDiscovered d 
			   inner join AAUTONetworkDependencies as e on d.src = e.NetworkID	
			
		   where not exists (select 1 from #AAutoDiscovered as dd 
						   where e.PreceedingNetworkID = dd.src and
							    e.NetworkID = dd.tgt)
		   group by e.PreceedingNetworkID, e.NetworkID
	   END

       SELECT * FROM #AAutoDiscovered 
	
       DROP TABLE #AAutoDiscovered

END