title: Adding a surrogate key to existing table (SQL SERVER)
body: <p>I have a large table of 225 million rows. It does NOT have any primary keys. It's just a sheet of data.</p>
  
  <p>I want to add a TableID, and set surrogate keys to it. I've created the <code>TableID bigint</code> column.</p>
  
  <p>What's the fastest way to put in key values (1, 2,3, etc...)?</p>
  
  <p>This is just horrendous as the optimizer sees it.</p>
  
  <pre><code>with CTE as (
      select ROW_NUMBER() 
        OVER (ORDER BY GEOID, A_ID, Zip, latitude, longitude) as rn,
      GID,
      A_ID,
      Zip,
      latitude,
      longitude from tableA
  ) update Table a set a.TableID = CTE.rn 
      where a.GID = CTE.GID
      and a.A_ID = CTE.A_ID
      and a.Zip = CTE.Zip
      and a.latitude = CTE.latutude
      and a.longitude = CTE.longitude;
  </code></pre>
  
  <p>Thanks.</p>