Mar 17, 2014

ORDER BY clause in view in sql server


We cannot use ORDER BY clauses in view directly in sql server. For example:  

CREATE VIEW viewStudent
AS
SELECT * FROM Student ORDER BY 1

We will get error message like :

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.

Solution: 
We must have to use either TOP or FOR XML. For example:

CREATE VIEW viewStudent
AS

No comments:

Post a Comment