title: Updating temp table with loop
body: <p>I'm attempting to create an automated way to find the Min-Max range of certain data points. The problem is that when arranging the HUB data points (SLICs) in sequential order, many of the HUBs SLIC flows are interrupted by the SLICs of other HUBs. This provides a problem because the ranges overlap for many HUBs. Thus, I need to stop the range for the HUB name 1, start a second range for HUB name 2, then start another new range for HUB 1 again. To put in visual form:</p>
  
  <pre><code>HUB 1: 0101-1490
  HUB 2: 1500-1501
  HUB 1: 1510-1513
  </code></pre>
  
  <p>etc.,</p>
  
  <pre><code>+-------------------+------+
  |       HUB         | SLIC |
  +-------------------+------+
  | MEADOWLANDS_NJ_D  | 1471 |
  | MEADOWLANDS_NJ_D  | 1480 |
  | MEADOWLANDS_NJ_D  | 1481 |
  | MEADOWLANDS_NJ_D  | 1482 |
  | MEADOWLANDS_NJ_D  | 1490 |
  | WILLOW GROVE_PA_D | 1500 |
  | WILLOW GROVE_PA_D | 1501 |
  | MEADOWLANDS_NJ_D  | 1510 |
  | MEADOWLANDS_NJ_D  | 1512 |
  | MEADOWLANDS_NJ_D  | 1513 |
  | WILLOW GROVE_PA_D | 1520 |
  | MEADOWLANDS_NJ_D  | 1522 |
  +-------------------+------+
  </code></pre>
  
  <p>I have never attempted a loop before, so I have been experimenting, but I believe I am stuck in an infinite loop. What I am attempting is to create two temp tables, the first housing the raw data which will be used to insert and update into the second table with the HUB name and the MIN and MAX SLICs for that range. </p>
  
  <p>I would like the results to be as such: </p>
  
  <pre><code>+-------------------+------+------+
  |        HUB        | MIN  | MAX  |
  +-------------------+------+------+
  | MEADOWLANDS_NJ_D  | 0101 | 1490 |
  | WILLOW GROVE_PA_D | 1500 | 1501 |
  | MEADOWLANDS_NJ_D  | 1510 | 1513 |
  | WILLOW GROVE_PA_D | 1520 | 1520 |
  | MEADOWLANDS_NJ_D  | 1522 | 1522 |
  | WILLOW GROVE_PA_D | 1530 | 1540 |
  +-------------------+------+------+
  </code></pre>
  
  <p>My query:</p>
  
  <pre><code>DROP TABLE SLIC5
  CREATE TABLE SLIC5
  (UPS_HUB nvarchar (255) NOT NULL,
  SLIC nvarchar (255) NOT NULL,
  PK_SLIC int NOT NULL)
  
  INSERT INTO SLIC5
      SELECT TOP (50) UPS_HUB, SLIC, RANK () OVER (ORDER BY SLIC) PK_SLIC
      FROM SLIC
  
  ALTER TABLE SLIC5
  ADD PRIMARY KEY (PK_SLIC)
  
  DROP TABLE #tempRAW
  CREATE TABLE #tempRAW
  (UPS_HUB varchar(50), SLIC nvarchar(10))
  
  INSERT INTO #tempRAW (UPS_HUB, SLIC)
      SELECT UPS_HUB, SLIC
      FROM dbo.SLIC5
      ORDER BY PK_SLIC ASC
  
  DROP TABLE #RANGE_TABLE
  CREATE TABLE #RANGE_TABLE
  (UPS_HUB varchar (50), [MIN] nvarchar (10), [MAX] nvarchar (10))
  
  INSERT INTO #RANGE_TABLE (UPS_HUB,[MIN])
  SELECT TOP (1) UPS_HUB, SLIC
  FROM #tempRAW
  
  DECLARE @currentHUB varchar, @currentMinSLIC nvarchar, @tempTableRecCount int
  SELECT @currentHUB=UPS_HUB, @currentMinSLIC=[MIN] from #RANGE_TABLE
  SELECT @tempTableRecCount= (SELECT count(*) from #tempRAW)
  
  WHILE (@tempTableRecCount &gt; 0)
  BEGIN
  DECLARE @loopingSLIC nvarchar, @loopingHUB varchar
  SELECT @loopingSLIC=SLIC, @loopingHUB=UPS_HUB from #tempRAW
      IF (@currentHub=@loopingHUB)
          BEGIN
              IF (@currentMinSLIC &lt; @loopingSLIC)
                  BEGIN
                      UPDATE #RANGE_TABLE
                      SET [MAX]=@loopingSLIC
                      WHERE #RANGE_TABLE.UPS_HUB=@currentHUB
                      END
              END
      IF (@currentHUB &lt;&gt; @loopingHUB)
          BEGIN
              INSERT INTO #RANGE_TABLE (UPS_HUB,[MIN])
                  VALUES (@loopingHUB,@currentMinSLIC)
      END
  
  DELETE FROM #tempRAW
  WHERE UPS_HUB=@loopingHUB and SLIC=@loopingSLIC
  
  SELECT @currentHUB=UPS_HUB from #tempRAW
  SELECT @currentMinSLIC= SLIC from #tempRAW
  
  SELECT @tempTableRecCount = count (*) from #tempRAW
  END
  
  SELECT * FROM #RANGE_TABLE
  SELECT * FROM #tempRAW
  </code></pre>