Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have the table structure as follows.
C#
sl.no     name        city
1          a           b
2          c           d
3          e           f
4          g           h

I have the above table. say i have totally 100 datas like that. Now in case lets consider i have deleted the 3rd row data. So the sl no will be like
C#
sl.no
1
2
4

Now what i want to do. I want the 3rd row sl no should be 3 irrespective of whatever the changes i make in the table. How can i accomplish this?
Posted
Updated 15-Sep-13 19:55pm
v2
Comments
TrushnaK 16-Sep-13 1:54am    
is your sl.no field is auot-increamented?
syed shanu 16-Sep-13 1:57am    
Chk this .

SELECT ROW_NUMBER() OVER(ORDER BY sl.no asc) AS SNO,
name city
FROM yourtablename
kparun86 16-Sep-13 2:08am    
OK will try that.

This is a bad idea: if you are using an Identity field for your I'd! Then there could be other fields using this informally as a foreign key, so SQL does not reuse or reassign numbers.

Instead, return the rownumber
SELECT ROW_NUMBER() OVER (ORDER BY id) as RowNum, [name], city FROM MyTable
 
Share this answer
 
Comments
kparun86 16-Sep-13 2:09am    
Ok fine. i have a huge database which is containing 60,000 datas. We may delete few datas and update few data. we have to maintain the continuity in the serial no column. Thats why i want to update like that.
OriginalGriff 16-Sep-13 4:27am    
But that's the whole point - renumbering items doesn't "maintain the continuity" - it destroys it! It isn't good for database integrity either: suppose you have two people who each delete a different item. Who gets to update the records?
And think of it: you delete record 2 - you update 59,997 records, which is a non-trivial operation.
It won't makes sense as there are two possibilities.

1) If its an identity field than integrity matters.

2) If its not an identity field,than you have to update all the values. That is not a good idea.

I am still not clear why you want such approach.

Regards..:)
 
Share this answer
 
v2
Comments
kparun86 16-Sep-13 2:09am    
Ok fine. i have a huge database which is containing 60,000 datas. We may delete few datas and update few data. we have to maintain the continuity in the serial no column. Thats why i want to update like that.
Thanks7872 16-Sep-13 2:22am    
That is what my point is.Why you want continuity? If its just for display purpose than you can handle it at the time of display. e.g. using datagrid to display records.
Dholakiya Ankit 16-Sep-13 2:28am    
yes.agree
kparun86 16-Sep-13 2:28am    
Continuity in the sense we want a total count of how many data's are there. thats why i wanted to update it like that.
Thanks7872 16-Sep-13 2:31am    
Then count the rows in database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900