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

Sort order in SQL 2005

 
   Database Help (Home) -> Programming RSS
Next:  Transactions in general  
Author Message
robin9876

External


Since: Sep 14, 2006
Posts: 11



(Msg. 1) Posted: Mon Nov 27, 2006 7:32 am
Post subject: Sort order in SQL 2005
Archived from groups: microsoft>public>sqlserver>programming (more info?)

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 specified?

 >> Stay informed about: Sort order in SQL 2005 
Back to top
Login to vote
Anith Sen

External


Since: Feb 17, 2004
Posts: 310



(Msg. 2) Posted: Mon Nov 27, 2006 9:52 am
Post subject: Re: Sort order in SQL 2005 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The order by is determined by the ORDER BY clause specified in the outer
query. Having an ORDER BY clause in an inline table valued function used in
another query does not guarantee ordered results for the final resultset.

For some related info, see:
http://blogs.msdn.com/sqltips/archive/2005/07/20/441053.aspx

--
Anith

 >> Stay informed about: Sort order in SQL 2005 
Back to top
Login to vote
Roy Harvey

External


Since: May 03, 2006
Posts: 270



(Msg. 3) Posted: Mon Nov 27, 2006 10:54 am
Post subject: Re: Sort order in SQL 2005 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 27 Nov 2006 07:32:39 -0800, wrote:

>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 specified?

The change is to add an ORDER BY to the SELECT that retrieves the rows
from the table function. The only way to ever be completely sure of
the order in which rows are returned has always been an explicit ORDER
BY.

Though SQL Server 2000 generally returned the rows in order when that
trick was used, it was never a documented behavior, just a trick. The
undocumented behavior changed with SQL Server 2005.

Roy Harvey
Beacon Falls, CT
 >> Stay informed about: Sort order in SQL 2005 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 4) Posted: Mon Nov 27, 2006 10:55 am
Post subject: Re: Sort order in SQL 2005 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> In a 2005 database table function that has SELECT TOP 100 Percent ....
> ORDER BY TextField

Do you mean a VIEW?

> This displays it in the primary key (numeric field) order and not the
> specified.

Do you mean if you say SELECT * FROM VIEW and don't bother using an ORDER BY
statement?

> Is there a setting change that is required to make the
> function return the order as specified?

Yes, you say SELECT * FROM View ORDER BY <specified order>. Views and
tables are, by definition, unordered sets of rows, and the optimizer is free
to return the rows in any order it deems appropriate, unless you explicitly
tell it what order you want. You do that by appending an ORDER BY clause to
the query, not to the definition of the view.

Even in the kludge case where a view allows an order by (which it shouldn't,
IMHO) -- this determines which top n rows are returned, but not the order in
which those rows are returned. The problem is actually in the dependency
between TOP <-> ORDER BY and how the syntax works, which would make more
sense if the selection of the rows were tied more tightly with TOP. ORDER
BY should be completely independent of that construct (again IMHO) and
shouldn't serve two purposes.
 >> Stay informed about: Sort order in SQL 2005 
Back to top
Login to vote
ANTIMS




Joined: May 04, 2009
Posts: 1



(Msg. 5) Posted: Mon May 04, 2009 4:36 pm
Post subject: Re: Sort order in SQL 2005 [Login to view extended thread Info.]

Looks and sounds to me like people can't read or listen.... So, did you ever find the solution to the vew not returning the sort order when you DO have an order by clause?

I found an article about an SP2 hotfix, of course I've already put on SP3, expecting the hotfix to be "in" SP3.. but no... back to the old NT4 scenario where you can't trust MS service packs again...
 >> Stay informed about: Sort order in SQL 2005 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
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

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 and view - 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...

Order by [alias] in 2005 issue - So this 'order by' works in SQL2000, but will error in 2005 - select top 1 a.forderID [orderId] from MyTable a order by a.orderid SQL2005 eRR: Msg 207, Level 16, State 1, Line 2 Invalid column name 'orderid'. How to work around? Is there a server...

"order by" in SQL 2005 vs. SQL 2000 or other issue? - I am trying to find any issue why the data is not coming out as it is intended. I converted table and stored procedure from SQL 2000 to SQL 2005. However, on several reports, data is not listed as it is intended by "order by" clause. I guess o...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
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 ]