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 2005 Forums
 Transact-SQL (2005)
 ORDER BY - Very quick question

Author  Topic 

im1dermike
Posting Yak Master

222 Posts

Posted - 2008-05-16 : 15:57:55
I have a view that I want to display in a particular order so I added "ORDER BY <column> DESC" to the end of my view logic. When I run it as a query or even when I execute the view in the modify window, it appears correctly ordered. But, when I just "Open View", it's not ordered how I want it. I don't know why it doesn't order it when you open the view, but how can I resolve this issue?

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-16 : 16:04:13
Because you aren't guaranteed order unless ORDER BY is in your SELECT. It doesn't matter if the ORDER BY is in your view.

SELECT ...
FROM YourView
ORDER BY YourColumn

YourView could have an ORDER BY, but it doesn't matter. What matters is the SELECT query calling the view.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

im1dermike
Posting Yak Master

222 Posts

Posted - 2008-05-16 : 16:43:11
I see.

I'm populating data in an Excel worksheet and I want it to be in a particular order. My SELECT statement pulls data from my view for an OPENROWSET call. I've altered the statement, adding the bold code below per your suggestion, but it still isn't coming out in any sort of order when I open the Excel file.

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:\TEMP.xls;','SELECT * FROM [WSC 2nd day App$]') select * from SQL_TEMP1 order by [Days Referral Open] DESC

What gives?
Go to Top of Page
   

- Advertisement -