Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Sort order and view

 
   Database Help (Home) -> Programming RSS
Next:  Collation Problem  
Author Message
Robert Bravery

External


Since: Nov 03, 2006
Posts: 17



(Msg. 1) Posted: Wed Jan 17, 2007 7:49 am
Post subject: Sort order and view
Archived from groups: microsoft>public>sqlserver>programming (more info?)

HI all,

Using SQL 2005, I create a simple view
Select * from employee order by name

But when I select the view with
select * from employeeview the sort order is not adheered to. I have to
specificcally issue a order by on the view

Can someone explain this to me

Thanks
Robert

 >> Stay informed about: Sort order and view 
Back to top
Login to vote
David Portas

External


Since: Nov 11, 2003
Posts: 854



(Msg. 2) Posted: Wed Jan 17, 2007 7:49 am
Post subject: Re: Sort order and view [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Robert Bravery wrote:
> HI all,
>
> Using SQL 2005, I create a simple view
> Select * from employee order by name
>

Views are like tables - they are unordered sets by definition.

ORDER BY is permitted in a view only when TOP is used. In that case the
purpose of ORDER BY is purely to define the subset of rows returned by
TOP, not to define any sort order (agreed, this is a really stupid and
confusing syntax).

> But when I select the view with
> select * from employeeview the sort order is not adheered to. I have to
> specificcally issue a order by on the view

As with any query, unless you specify ORDER BY the expected result is
logically unordered. The optimizer is free to return the result in any
order at all.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

 >> Stay informed about: Sort order and view 
Back to top
Login to vote
David Portas

External


Since: Nov 11, 2003
Posts: 854



(Msg. 3) Posted: Wed Jan 17, 2007 7:49 am
Post subject: Re: Sort order and view [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Rob Farley wrote:
> A view shouldn't be ordered. You should order when you select from the view.
> You can get around it by using the top clause like this:
>
> select top (99999999) from employee order by name
> --But I strongly recommend you don't do this, as it's very bad practice.
>

I also strongly recommend you don't do it. It doesn't work for all
queries (probably not for most queries in fact) and I'm not aware of
any means to predict when it will work. Even if it works today it will
probably break under a future release.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
 >> Stay informed about: Sort order and view 
Back to top
Login to vote
Rob Farley

External


Since: Aug 08, 2006
Posts: 17



(Msg. 4) Posted: Wed Jan 17, 2007 5:10 pm
Post subject: Re: Sort order and view [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

A view shouldn't be ordered. You should order when you select from the view.
You can get around it by using the top clause like this:

select top (99999999) from employee order by name
--But I strongly recommend you don't do this, as it's very bad practice.

--
Rob Farley
President - Adelaide SQL Server User Group
(Email & Msgr)
http://msmvps.com/blogs/robfarley

"Robert Bravery" wrote in message

> HI all,
>
> Using SQL 2005, I create a simple view
> Select * from employee order by name
>
> But when I select the view with
> select * from employeeview the sort order is not adheered to. I have to
> specificcally issue a order by on the view
>
> Can someone explain this to me
>
> Thanks
> Robert
>
>
 >> Stay informed about: Sort order and view 
Back to top
Login to vote
Robert Bravery

External


Since: Nov 03, 2006
Posts: 17



(Msg. 5) Posted: Wed Jan 17, 2007 5:10 pm
Post subject: Re: Sort order and view [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks

Robert

"Rob Farley" wrote in message

>A view shouldn't be ordered. You should order when you select from the
>view. You can get around it by using the top clause like this:
>
> select top (99999999) from employee order by name
> --But I strongly recommend you don't do this, as it's very bad practice.
>
> --
> Rob Farley
> President - Adelaide SQL Server User Group
> (Email & Msgr)
> http://msmvps.com/blogs/robfarley
>
> "Robert Bravery" wrote in message
>
>> HI all,
>>
>> Using SQL 2005, I create a simple view
>> Select * from employee order by name
>>
>> But when I select the view with
>> select * from employeeview the sort order is not adheered to. I have to
>> specificcally issue a order by on the view
>>
>> Can someone explain this to me
>>
>> Thanks
>> Robert
>>
>>
>
>
 >> Stay informed about: Sort order and view 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Help with distinct + sort order - Hi I've the following set of un-normalised records. SortID Customer 1 ccc 2 ccc 3 aaa 4 aaa 5 bbb 6 bbb I need to perform a select which will return distinct Customer name while ..

Sort order in SQL 2005 - In a 2005 database table function that has SELECT TOP 100 Percent .... ORDER BY TextField This displays it in the primary key (numeric field) order and not the specified. Is there a setting change that is required to make the function return the order as...

Sort order in SQL 2005? - Hi, When sorting a numeric field in SQL 2005, I am finding that it is sorting it differently than it was in 2000. i.e. 1, 10, 1000, 1001, 1002, 2, 20, 2000, 2002. Is there a reason for this? Appreciate the help. Damon

why can't ORDER BY be used in a view - Hello Again: After having almost completed my project, SQL is giving me an error message upon editing my view saying that "the ORDER BY clause cannot be used in a view unelss TOP is used". I am using TOP. If you have any ideas, please let ...

Problem when creating an Indexed View (View Uses CTE with .. - Hi, I am trying to create an Indexed View using CTE with recursion. (Not sure if I could do that). Here is my query : CREATE VIEW ContactUserHierarchyView WITH SCHEMABINDING AS ( WITH ContactUserHierarchy (ParentContact_Id, Contact_Id, Person_Id, ..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]