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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to populate a newly added column?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

2 Posts

Posted - 02/18/2011 :  18:19:11  Show Profile  Reply with Quote
I have a table with the following fields:
StudentNo, Class, Grade. I added a new column called sequence. I have close to 5000 records to update. How do I populate the column so that the table will look like below?

Student Class Sequence Grade
1 A 1 80
1 B 2 75
1 C 3 82
2 D 1 72
2 E 2 86

Thank you so much.


Flowing Fount of Yak Knowledge

1182 Posts

Posted - 02/19/2011 :  00:17:39  Show Profile  Reply with Quote
You have not mentioned what version of SQL you are using. If you are using 2005 or higher then the below logic should work for you.

--Sample table structure.
Create table #Students
StudentNo int,
Class varchar(50),
Grade int

Insert into #Students
Select 1,'A', 80 union all
Select 1 ,'B', 75 union all
Select 1 ,'C',82 union all
Select 2 ,'D',72 union all
Select 2 ,'E', 86

Alter table #Students
Add Seq int

--Actual Logic for updation starts here

;with cte
Select Row_number() Over (Partition by StudentNo order by StudentNo) as Srno,
* from #Students

update cte set seq = srno

select * from #Students

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000