Results 1 to 4 of 4
  1. #1
    Join Date
    May 2012
    Posts
    3

    Unanswered: How To Generate/populate already existing column with sequential data

    I have a table which is already populated with data (Microsoft SQL 2008). I have now created a new column (int) which i want to populate with sequential numbers so that the new column created will serve let me know how many records exist in the table at a glance.

    Can any one help as to what SQL statement I need to write that will automatically polulate the newly created column with 1,2,,3,4,5 etc so that I can sort of number the records within the table.
    I have 50000 records which I need to number and I really dont want to number the column manually via hand editing.

    Thanks in advance

  2. #2
    Join Date
    Apr 2012
    Posts
    213
    Try something like this:

    Code:
    with CTE as
    (
        SELECT NewColumnInt, ROW_NUMBER() OVER(ORDER BY OrderColumn) AS RowNo
        FROM MyTable
    )
    
    update CTE set NewColumnInt = RowNo
    Hope this helps.

  3. #3
    Join Date
    May 2012
    Posts
    3
    Thankyou for your help I will implement your suggestion. I am much obliged.

  4. #4
    Join Date
    May 2012
    Posts
    3

    Smile

    I just want to say thank you once again - i tried the code and it worked perfectly. Thanks a million.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •