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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 "order by" not working in query?!

Author  Topic 

_mark_
Starting Member

2 Posts

Posted - 2007-04-05 : 18:01:57
hi i'm new here and i'm hoping one of you clever people can help me with my problem!

right, I may have gone crazy and I'm missing something blindingly obvious but here goes...

I have a db running on ms sql server 2005

on this db, if i create a query to manipulate data from a table - for example -

select * from tblUser order by id desc

- and then save this query ,for example new_query, and then run the query with the following command -

select * from new_query

- it seems to completely ignore the "order by" part of the query and returns the results unsorted!! I have tried creating other similar queries using other tables and they all ignore the "order by" section! i can kind of get round this by using the command -

select * from new_query order by id desc

- but this seems a little strange as i thought the query should do this for me?!!?

I hope that makes sense and if anyone could shed some light on this issue i will be forever in you debt,

If anyone needs any more info let me know,

Kind Regards,

Mark

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-05 : 18:07:10
"Save query"? What is that? Did you create a VIEW? If so, it should work.
If you "saved the query" by inserting the data into a new table, there is no guarantee that the next select from the new table fetches the records in order.

This is one of the cornerstones in relational database management.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

_mark_
Starting Member

2 Posts

Posted - 2007-04-05 : 18:09:18
yeh sorry i "created of view" - my bad terminology (i'm fairly new to all this!)
Go to Top of Page

Raibeart
Starting Member

8 Posts

Posted - 2008-04-23 : 07:49:55
This is a known bug in 2005. There is a post-SP2 hotfix for it.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-04-23 : 13:53:29
Sets do not have order and since views are sets they do not have order either. You need to use an ORDER BY on the your select from the view not in the view definition. You can use an ORDER BY in the view in conjunction with a TOP clause, but that is only to limit the results to some number or percent of records. Check out Books Online for all the gory details.
Go to Top of Page
   

- Advertisement -