sql-serversql-server-2012sequential-number

SQL Server query add sequential number to resuls


I have emergency contact information attached to contact records that I am making into their own contact records.

I need to assign a temporary id to the new contact records for migration purposes.

I was hoping to do this within my SQL query that retrieves the data for creating the new records.

I found references to updating the SQL table to add a new column, but that's not what I want. I can do it after in Excel, but would prefer a one step process. Is there a way to do this?


Solution

  • If this is a one time thing you can use row_number()

    SELECT 
      *,
      ROW_NUMBER() OVER (ORDER BY SomeColumn) as TempID
    FROM YourTable
    WHERE...