| Author |
Topic |
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 07:56:27
|
| Hi! I am having some challenges in the table I am working on. I have 3 columns (s_num, s_entity, s_name) and the data in the last 2 columns I have copied in a different table. So the challenge is I have to number the first column which is s_num. I have tried using the following code but did not work.DECLARE @count intset @count = 1while @count < 247begin update entity set s_num = @count set @count=@count+1endThe above code only gives me the total count of rows, what i need is the rows to be numbered, incrementing by 1.I have 246 rows and I need the s_num column to be numbered from 1...246.Thanks in advance! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 07:58:09
|
| just make s_num column as a identity column and it will autonumber itself as on insertion of values to other fields.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 07:58:59
|
| alternatively if you need to manually number it sequentially, have a look at ROW_NUMBER() function------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 08:02:10
|
| Hi visakh! I am really new in sql and all i have to do is number all rows to reflect in the s_num column. Any idea on what code should i execute? Am I on the right track in the code I posted here? :( |
 |
|
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 08:04:50
|
| Some are saying I have to use cursor. Need help on how to execute code using cursor to increment rows by 1 and finally reflect the numbers in my s_num column |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 08:04:53
|
| have you already inserted or are you about to insert?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 08:06:58
|
| the row insertion will happen in a different program. I just have to number the rows in my database. currently I have 246 rows and I have to number it from 1...246. :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 08:09:25
|
ok then it should beUPDATE tSET s_num=rnFROM (SELECT ROW_NUMBER() OVER (ORDER BY s_name) AS rn,s_num FROM entity)t I'm assuming you've no control over table schema otherwise by all means alter table to make s_num an IDENTITY column------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 08:23:16
|
| Hi again! I have tried to use the query but it gives me an error -Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'from'.:( |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 08:27:58
|
| post used code------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 08:31:02
|
| oops. late posting of the code :Dhere it is:update entity set s_num = rnfrom (select ROW_NUMBER() over (order by s_entity_name)) as rn, s_num from entity)thank you so much for helping :D |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 08:34:31
|
quote: Originally posted by freshman11 oops. late posting of the code :Dhere it is:update tset s_num = rnfrom (select ROW_NUMBER() over (order by s_entity_name)) as rn, s_num from entity)tthank you so much for helping :D
please try to use exact posted code. your above code is different see edited one------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2011-07-20 : 08:35:11
|
quote: Originally posted by freshman11 oops. late posting of the code :Dhere it is:update entityEset s_num = rnfrom (select ROW_NUMBER() over (order by s_entity_name)) as rn, s_num from entity) Ethank you so much for helping :D
Corey I Has Returned!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 08:37:46
|
quote: Originally posted by Seventhnight
quote: Originally posted by freshman11 oops. late posting of the code :Dhere it is:update entityEset s_num = rnfrom (select ROW_NUMBER() over (order by s_entity_name)) as rn, s_num from entity) Ethank you so much for helping :D
Corey I Has Returned!!
It still has an additional closing braces------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 08:45:19
|
| Hi again! I have used the below code:UPDATE entitySET s_num=rnFROM (SELECT ROW_NUMBER() OVER (ORDER BY s_entity_name) AS rn,s_num FROM entity)and now the error is -> Msg 102, Level 15, State 1, Line 4Incorrect syntax near ')'. :( |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 08:46:41
|
| Can you please copy paste my suggestion and use it? For Gods sake dont make any amendments------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
freshman11
Starting Member
13 Posts |
Posted - 2011-07-20 : 08:58:52
|
| Got it! Thanks so much! :D |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-20 : 09:03:10
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|