Microsoft KB Archive/926292

= FIX: When you query through a view that uses the ORDER BY clause in SQL Server 2005, the result is still returned in random order =

Article ID: 926292

Article Last Modified on 11/4/2007

-

APPLIES TO


 * Microsoft SQL Server 2005 Standard Edition
 * Microsoft SQL Server 2005 Developer Edition
 * Microsoft SQL Server 2005 Workgroup Edition
 * Microsoft SQL Server 2005 Enterprise Edition
 * Microsoft SQL Server 2005 Express Edition

-



Bug #: 50000305 (SQL Hotfix)



Microsoft distributes Microsoft SQL Server 2005 fixes as one downloadable file. Because the fixes are cumulative, each new release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2005 fix release.



This article describes the following about this hotfix release:
 * The issues that are fixed by the hotfix package
 * The prerequisites for applying the hotfix package
 * Whether you must restart the computer after you apply the hotfix package
 * Whether the hotfix package is replaced by any other hotfix package
 * Whether you must make any registry changes after you apply the hotfix package
 * The files that are contained in the hotfix package



SYMPTOMS
You have a view in a database in SQL Server 2005. In the definition of the view, the SELECT statement meets the following requirements:
 * The SELECT statement uses the TOP (100) PERCENT expression.
 * The SELECT statement uses the ORDER BY clause.

When you query through the view, the result is returned in random order.

However, this behavior is different in Microsoft SQL Server 2000. In SQL Server 2000, the result is returned in the order that is specified in the ORDER BY clause.



RESOLUTION
Two versions of this hotfix exist. One version is designed to be applied to the release version of SQL Server 2005. The other version is designed to be applied to SQL Server 2005 Service Pack 2 (SP2). If you are running the release version of SQL Server 2005, apply the hotfix that is described in this article (build 1554). If you are running SQL Server 2005 SP2, apply cumulative update package 2 for SQL Server 2005 SP2 (build 3175). If you are running SQL Server 2005 Service Pack 1 (SP1), install SQL Server 2005 SP2, and then apply cumulative update package 2 for SQL Server 2005 SP2 (build 3175). Cumulative update package 2 for SQL Server 2005 SP2 (build 3175) is described in Microsoft Knowledge Base article 936305. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

936305 Cumulative update package 2 for SQL Server 2005 Service Pack 2 is available

Hotfix information
A supported hotfix is now available from Microsoft. However, this hotfix is intended to correct only the problem that this article describes. Apply this hotfix only to systems that are experiencing this specific problem.

To resolve this problem, submit a request to Microsoft Online Customer Services to obtain the hotfix. To submit an online request to obtain the hotfix, visit the following Microsoft Web site:

http://go.microsoft.com/?linkid=6294451

Note If additional issues occur or any troubleshooting is required, you might have to create a separate service request. The usual support costs will apply to additional support questions and issues that do not qualify for this specific hotfix. To create a separate service request, visit the following Microsoft Web site:

http://support.microsoft.com/contactus/?ws=support

Prerequisites
You must have installed the release version of SQL Server 2005 to apply this hotfix.

Restart information
You must restart the SQL Server service after you apply this hotfix.

Registry information
You do not have to change the registry after you apply this hotfix.

Actions after you apply this hotfix
This hotfix introduces trace flag 168. After you apply this hotfix, you must enable trace flag 168. For information about how to enable a trace flag in SQL Server 2005, see the &quot;Remark&quot; section of the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms188396.aspx

Additionally, you must change the compatibility level of the database that contains the view to 80.

To change the compatibility level, follow these steps:
 * 1) Open SQL Server Management Studio, and then connect to the instance of SQL Server 2005.
 * 2) Right-click the database that contains the view, and then click Properties.
 * 3) In the Select a page pane, click Options.
 * 4) In the Compatibility level list, select SQL Server 2000 (80).

Hotfix file information
This hotfix contains only those files that are required to correct the issues that this article lists. This hotfix may not contain all the files that you must have to fully update a product to the latest build.

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time item in Control Panel.



STATUS
This behavior is by design. This behavior in SQL Server 2005 will remain in the future releases of SQL Server.



MORE INFORMATION
If you apply this hotfix to SQL Server 2005 before you migrate an application from SQL Server 2000 to SQL Server 2005, this hotfix will make the application work with SQL Server 2005. For example, you expect to obtain the data in a specific sorted order. To do this, you must use the ORDER BY clause in the SELECT statement that you use to query through the view. After you apply this hotfix and migrate the application, you will obtain a sorted query result.

However, this hotfix is only a temporary resolution. After you migrate the application, you must update the application to work correctly with the new behavior in SQL Server 2005.

After you update the application, you can disable trace flag 168 and change the compatibility level of the database back to 90.

