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
 Ordering Ids

Author  Topic 

shajimanjeri
Posting Yak Master

179 Posts

Posted - 2007-06-18 : 07:58:12

I have a table named as C1_Messages with fields Id, Messages, Dates.
My Id filed looks like this:
1000
1001
1008
1009
1084
1093
1098 etc.

But here I need to make these Ids in a order, means it sould be in a consecutive order. Like
1000
1001
1002
1003
1004
1005 etc.

So is there anyway to do this method in SQL?

Regards
Shaji

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-18 : 08:05:42
Sure. You can use ORDER BY clause in your SELECT statement.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

shajimanjeri
Posting Yak Master

179 Posts

Posted - 2007-06-18 : 08:54:35
But I need the Ids to be update and set all these Ids in consecutive orders. Like 1000 for first row, 1001 for second row, 1003 for third row, 1004 for fourth row ete to last record.
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2007-06-18 : 23:21:33
In SQL Server 2005 you can probably put together an update statement from a select of your existing IDs plus the row_number() function.
Not something I'd recommend though. An ID is hardly and ID if it changes.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-06-19 : 02:50:21
try this (untested)

UPDATE t
set t.ID = t.recid
from (select 999 + row_number() over (order by id) as recid from C1_Messages ) as t


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-20 : 05:23:27
Where do you want to show data?
Sounds that you want to generate Serial No

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -