[dba-SQLServer] Views don't sort

jwcolby jwcolby at colbyconsulting.com
Fri May 20 12:26:15 CDT 2011


Francisco,

The sql from the view.

SELECT     TOP (100) PERCENT INM_ID, INM_LName + ', ' + INM_FName AS Name
FROM         dbo.tblInmate
WHERE     (INM_Active <> 0)
ORDER BY Name

This specific view is sorting ascending on the name field (calculated obviously).

It does return sorted if I open it in design view and then bang the query.

32	BROWN, ANTHONY
35	CHISGAR, DENNIS
29	COCHRAN, ROBERT
33	COLLINS, WILLIE
36	EDMISTEN, FRED
37	EDWARDS, CASEY
6	ERVIN, DAVID

but if I open a query and do select * from QryXYZ

INM_ID	Name
4	HUSH, KEITH
5	HODSDEN, MARK
6	ERVIN, DAVID
10	PRATT, BAXTER
11	POWELL, BOBBY
15	TURNER, CHARLES
17	HARDISON, DAVID
19	JONES, JEFFREY

It appears that the query is getting the data sorted by the PKID (which has a clustered index on 
it).  Back in access I get the same "ordered by PKID" order.

John W. Colby
www.ColbyConsulting.com

On 5/20/2011 1:07 PM, Francisco Tapia wrote:
> So something like:
>
> Create View vwSomeView AS
> Select TOP 99.9999 percent Field1, Field2, Field3
>> From tblSomeTable
> Order by Field3
>
> does not sort field3?  what are your results when you just select * from
> vwSomeView ?  are the results sorted in your results display in management
> studio?
>
>
> -Francisco
> <http://bit.ly/sqlthis>
>
>
>
> On Fri, May 20, 2011 at 10:02 AM, jwcolby<jwcolby at colbyconsulting.com>wrote:
>
>> One of the things I am trying to do is use SQL Server to speed up my
>> applications.  The theory is that I can hand off the heavy lifting to SQL
>> Server and just get back result sets.  Of course this works in terms of
>> joins and filters in a view, but even though I specify a sort in a view,
>> when the result set hits the other end (Access in my case) it is unsorted.
>>
>> Views have the ability to do sorts, so why is the data returned by a view
>> into a third party app, or even into another view in SQL Server unsorted?
>>   Is there a way to tell sql server to return sorted data?
>>
>>
>>
> _______________________________________________
> dba-SQLServer mailing list
> dba-SQLServer at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/dba-sqlserver
> http://www.databaseadvisors.com
>
>



More information about the dba-SQLServer mailing list