Technical Article

Adding Sorts to SQL Server Views

,

Microsoft says that you cannot add an ORDER BY to a view unless it comes with a TOP clause at the beginning of the query. Many people think that if that's true then you cannot "Order" the whole query but only the top few. However, this is not true because when you use TOP 100 PERCENT with the query you can ORDER BY the whole result set inside the view definition, like this (I tested this on a NT4 workstation and on NT4 server SP6, with SQL Server versions 7 and 2000):

CREATE VIEW  OrdersDetailsPlusOrderBy 
AS 
SELECT 
        TOP 100 PERCENT 
        OrderId, 
        ProductId, 
        UnitPrice, 
        Quantity 
FROM 
        northwind..[order Details] 
ORDER BY 
        UnitPrice DESC

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating