FIX: Incorrect Results Are Returned When You Use ROWCOUNT and an INSERT INTO Statement (317847)



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)

This article was previously published under Q317847
BUG #: 356639 (SHILOH_BUGS)

SYMPTOMS

If you use a SET ROWCOUNT statement to limit the results returned from a query, and the INSERT INTO statement contains a SELECT statement with an ORDER BY clause, the query may return incorrect data.
For example, a query may look similar to:
 
      SET ROWCOUNT 5

      INSERT INTO tbl2_name(myid)
      SELECT myid
      FROM tbl1_name
      ORDER BY myid

      SET ROWCOUNT 0
				
The results returned from such a SELECT statement may be incorrect.

RESOLUTION

To resolve this problem, obtain the latest service pack for Microsoft SQL Server 2000. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

290211 INF: How To Obtain the Latest SQL Server 2000 Service Pack

NOTE: The following hotfix was created before the release of Microsoft SQL Server 2000 Service Pack 3.

The English version of this fix must have the following file attributes or later:
   Date         Version      Size         File name
   ---------------------------------------------------

   1/31/2002    8.00.0576    7,269 KB     Sqlservr.exe
				
NOTE: Because of file dependencies, the most recent hotfix or feature that contains the files may also contain additional files.


WORKAROUND

One of the ways to limit the rows returned in a result set is to use the TOP keyword in the SELECT statement. If you modify the query to use the TOP keyword instead of ROWCOUNT, the query works correctly.

For example, you can modify the sample query like this:
 
      INSERT INTO tbl2_name(myid)
      SELECT TOP 5 myid
      FROM tbl1_name
      ORDER BY myid
				

STATUS

Microsoft has confirmed that this is a problem in Microsoft SQL Server 2000.
This problem was first corrected in Microsoft SQL Server 2000 Service Pack 3.

Modification Type:MinorLast Reviewed:9/27/2005
Keywords:kbHotfixServer kbQFE kbbug kbfix kbSQLServ2000preSP3fix KB317847 kbAudDeveloper