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
 Order By no longer sorting

Author  Topic 

thenoodle
Starting Member

1 Post

Posted - 2009-04-22 : 19:20:45
Hello everyone,

First off, I am new to the SQL Team forums. I have been working with simple SQL queries in Access for a couple years now, but I am starting to work with MS SQL Server 2005 as I work more with our company's website. I decided to search for a community of fellow SQL Server users and I came across you guys.

So now to my newbie problem - probably a quick fix :)

I have a view that pulls a distinct list of manufacturers from table [Products] and suppresses a selection from [tbl_mfgstuff]. This view is referenced by our website for a drop-down to browse manufacturer's in ascending order.

SELECT DISTINCT TOP (100) PERCENT Manufacturer
FROM dbo.products
WHERE (Manufacturer IS NOT NULL) AND (Store_ID = 1) AND (Active = 1) AND (NOT (Manufacturer IN
(SELECT Manufacturer AS manufacturer
FROM dbo.tbl_mfgstuff
WHERE (noBrowse = 1))))
ORDER BY Manufacturer

This was working fine until I made some changes yesterday. I updated approx 900 records with new Manufacturer values in [products]. Now the view does not output in ascending order. When I use "Show Results" in Visual Studio, the output is incorrect. However, when I "Execute SQL" in Visual Studio, the output is correct.

Is this a quirk with some type of cache or index?

Any help is appreciated, Thanks!
-Matt

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-22 : 19:32:37
You can not guarantee a sorting order by adding an ORDER BY in a view. You must do it while you are querying the view:

SELECT *
FROM YourView
ORDER BY Manufacturer

So remove the TOP 100 PERCENT and ORDER BY junk and then do the ORDER BY when you query it.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -