Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SELECT ID, name, city, ImageName, ImagePath
FROM aaaa
ORDER BY CASE WHEN ID > 0 THEN
(SELECT MAX(ID)
FROM [dbo].[aaaa]) + 1 ELSE ID END, ID DESC


result
id
0
3
2
1
Posted
Updated 11-Apr-13 2:56am
v6
Comments
AmitGajjar 11-Apr-13 8:25am    
are you sure this query is correct ? it doesn't surve any purpose.
chinmaya parija1 11-Apr-13 8:42am    
yes
the result like this
ID
0
3
2
1
chinmaya parija1 11-Apr-13 8:47am    
i want result like this
ID column
0
3
2
1
while desc and
ID column
0
1
2
3
while asc

0 id will be in top whether asc or desc.....

1 solution

If the row ID is greater than zero, then the query will return the maximum ID value, plus one.
If it isn't, it will return zero.

And it's a stupid thing to do. If it is used in a multiuser environment, it leads to IDs being duplicated occasionally, more frequently is there is any noticeable time period between the use of this code and the actual save of the new record. Intermittent problems like this are a real PITA to find once a database goes live, and should be avoided at all costs. Instead, allocate an id when you save the data, and not before - use the Identity specification if you have to, or use GUIDs instead.
 
Share this answer
 

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