Wednesday, 4 December 2019

ORDER BY clause in View


Order by clause is used for displaying the data in the sorted order either ascending order or descending order. When we are using order by clause in view definition it throws an error.
I am using AdventureWorksDW2012 database for the demo.
Need to create a view that displays the customer details on the older customer by date of the birth date.
 

Now I am creating a simple view.
 

I am not able to create this view. It throws the above error.
To overcoming this error we need to use TOP 100 PERCENT.
create view  vw_Customer_details
as
 SELECT TOP 100 PERCENT
                   CustomerKey     
      ,FirstName
      ,MiddleName
      ,LastName
      ,NameStyle
      ,BirthDate
      ,MaritalStatus
      ,Gender
      ,EmailAddress
  FROM DimCustomer
  order by BirthDate


 Now the view is created successfully.


See the view result set.
 

With a view, we can use the order.


Keep in mind: In view definition, we can’t use order by clause without the top clause.

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts