June 2008
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
Search

 
Catagories
Archives
Recent Entries
Links
RSS
ordering views
Catagory: sql server · This Entry · Comment(0) · eMail entry · Google
October 27, 2004 02:26 PM

sql server

'Order by', in SQL Views :

SQL Server usually doesn't allow an ORDER BY clause in views. However, you can use this workaround: Just add a TOP 100 PERCENT clause to the front of the SELECT list.


Here's an example of how to use TOP 100 PERCENT to order data in views. The Northwind database contains a view erroneously called Alphabetical list of products. If you open this view, you'll see that it doesn't show the products in alphabetical order.

To put the products in alphabetical order, you modify the SQL code as follows:

SELECT TOP 100 PERCENT Products.*,
Categories.CategoryName AS CategoryName
FROM Categories INNER JOIN
Products ON
Categories.CategoryID =
Products.CategoryID
WHERE (Products.Discontinued = 0)
ORDER BY ProductName

note: some sql syntax checkers may complain. you can safetly ignore "ORDER BY not allowed in this type of query" warnings in this case. it should still work, but test it out first to make sure.





Comments

Post a comment
Name:


Email Address:


URL:


Comments: