Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Add Sequence number to existing table

Author  Topic 

ugh3012
Yak Posting Veteran

62 Posts

Posted - 2013-11-14 : 14:07:02
I have a table with no sequence column. I created a new column, but I need a way to add sequence number. This is not going to be a Primary key or indexed.

I assume I will use update, but how do I do this?


Example

Before
Name | Seq
Paul | NULL
Tom | NULL
Grant | NULL

After
Name | Seq
Paul | 1
Tom | 2
Grant | 3

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-14 : 14:28:25
[code]
UPDATE t
SET Seq = Rn
FROM
(
SELECT Seq,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS Rn
FROM Table
)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -