SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to perform ORDER BY in a view?

The T-SQL ORDER BY clause is not allowed in view definition unless it includes the TOP, FOR XML or ROW_NUMBER() expressions.

When an ORDER BY create view attempted, following is the error message:

/* Msg 1033, Level 15, State 1, Procedure ww, Line 3
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and
common table expressions, unless TOP or FOR XML is also specified.

*/

The following Microsoft SQL Server T-SQL example scripts illustrate view definition with ORDER BY and usage. Note that only the ORDER BY clause in the outer SELECT query guarantees the desired sorting.

-- MSSQL create view with order by - T-SQL row number over order by
-- SQL inner join - left outer join - left join
USE AdventureWorks2008;
GO
CREATE VIEW vProductDetail
AS
  SELECT ROW_NUMBER()
           OVER(ORDER BY P.[Name])  AS RowID,
         P.[Name]                   AS ProductName,
         PM.[Name]                  AS ProductModel,
         PC.[Name]                  AS ProductCategory,
         PS.[Name]                  AS ProductSubCategory,
         PD.[DESCRIPTION],
         P.ProductNumber,
         COALESCE(P.Color,'')       AS Color,
         COALESCE(P.[Size],'')      AS [Size],
         COALESCE(P.[Weight],0.0)   AS [Weight],
         P.StandardCost,
         COALESCE(P.Style,'')       AS  Style,
         COALESCE(P.Class,'')       AS Class,
         P.ListPrice
  FROM   Production.Product P
         INNER JOIN Production.ProductSubcategory PS
         INNER JOIN Production.ProductCategory PC
           ON PS.ProductCategoryID = PC.ProductCategoryID
           ON P.ProductSubcategoryID = PS.ProductSubcategoryID
         INNER JOIN Production.ProductProductPhoto PPP
           ON P.ProductID = PPP.ProductID
         INNER JOIN Production.ProductPhoto PP
           ON PPP.ProductPhotoID = PP.ProductPhotoID
         LEFT OUTER JOIN Production.ProductDescription PD
         INNER JOIN Production.ProductModel PM
         INNER JOIN Production.ProductModelProductDescriptionCulture PCULT
           ON PM.ProductModelID = PCULT.ProductModelID
           ON PD.ProductDescriptionID = PCULT.ProductDescriptionID
           ON P.ProductModelID = PM.ProductModelID
  WHERE  (PCULT.CultureID = 'en')
GO
 
-- SQL view select results may be unordered despite the ordering in the view
SELECT * FROM   vProductDetail
GO
 
-- SQL view select results are ordered
SELECT * FROM   vProductDetail
ORDER BY RowID
GO
 
/* Partial results
 

RowID ProductName ProductModel ProductCategory ProductSubCategory
1 All-Purpose Bike Stand All-Purpose Bike Stand Accessories Bike Stands
2 AWC Logo Cap Cycling Cap Clothing Caps
3 Bike Wash - Dissolver Bike Wash Accessories Cleaners
4 Cable Lock Cable Lock Accessories Locks
5 Chain Chain Components Chains
6 Classic Vest, L Classic Vest Clothing Vests
7 Classic Vest, M Classic Vest Clothing Vests
8 Classic Vest, S Classic Vest Clothing Vests
9 Fender Set - Mountain Fender Set - Mountain Accessories Fenders
10 Front Brakes Front Brakes Components Brakes
11 Front Derailleur Front Derailleur Components Derailleurs
12 Full-Finger Gloves, L Full-Finger Gloves Clothing Gloves

 

*/
 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.