Microsoft KB Archive/822033

= FIX: A Parameterized UNION Query Inside a Stored Procedure Returns Incorrect Results =

Article ID: 822033

Article Last Modified on 11/2/2007

-

APPLIES TO


 * Microsoft SQL Server 2000 Developer Edition
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 2000 Enterprise Edition
 * Microsoft SQL Server 2000 Personal Edition
 * Microsoft SQL Server 2000 Enterprise Edition
 * Microsoft SQL Server 2000 Workgroup Edition
 * Microsoft SQL Server 2000 Desktop Engine (Windows)
 * Microsoft SQL Server 2000 Developer Edition
 * Microsoft SQL Server 2000 Enterprise Edition 64-bit

-





SYMPTOMS
When you run a stored procedure on a multi-processor computer, the stored procedure may return an incorrect number of rows, or incorrect results, if the following conditions are true:
 * The stored procedure has a query that runs in parallel.
 * The query creates a derived table and the table is the result of a UNION or a UNION ALL operation.
 * At least one branch of the UNION query uses a parameter value passed to the procedure as a constant expression in its SELECT list.
 * The derived table generated by the UNION query is joined with another table using a column that returns one of the constant parameter values passed to the procedure.
 * The query uses a hash join strategy that relies on partitioning by the join column.

This query is an example of the syntax that may cause the behavior to occur:

create procedure example_proc @P1 int as select * from ( select @P1 as x, ColB as y from T1         union all          select ColA as x, ColB as y from T2        ) as DerivedT Join T3    on DerivedT.x = T3.ColA go



Service pack information
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 How to obtain the latest SQL Server 2000 service pack

Hotfix information
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 tool in Control Panel.   Date         Time   Version         Size             File name ---  31-May-2003  18:45  2000.80.818.0      78,400 bytes  Console.exe 25-Jun-2003 01:01  2000.80.818.0      33,340 bytes  Dbmslpcn.dll 25-Apr-2003 02:12                    786,432 bytes  Distmdl.ldf 25-Apr-2003 02:12                  2,359,296 bytes  Distmdl.mdf 30-Jan-2003 01:55                        180 bytes  Drop_repl_hotfix.sql 23-Jun-2003 22:40  2000.80.837.0   1,557,052 bytes  Dtsui.dll 23-Jun-2003 22:40  2000.80.837.0     639,552 bytes  Dtswiz.dll 24-Apr-2003 02:51                    747,927 bytes  Instdist.sql 03-May-2003 01:56                      1,581 bytes  Inst_repl_hotfix.sql 08-Feb-2003 06:40  2000.80.765.0      90,692 bytes  Msgprox.dll 01-Apr-2003 02:07                      1,873 bytes  Odsole.sql 05-Apr-2003 01:46  2000.80.800.0      62,024 bytes  Odsole70.dll 07-May-2003 20:41  2000.80.819.0      25,144 bytes  Opends60.dll 02-Apr-2003 21:48  2000.80.796.0      57,904 bytes  Osql.exe 02-Apr-2003 23:15  2000.80.797.0     279,104 bytes  Pfutil80.dll 22-May-2003 22:57                     19,195 bytes  Qfe469571.sql 11-Jul-2003 17:04                  1,084,147 bytes  Replmerg.sql 04-Apr-2003 21:53  2000.80.798.0     221,768 bytes  Replprov.dll 08-Feb-2003 06:40  2000.80.765.0     307,784 bytes  Replrec.dll 11-Jul-2003 16:56                  1,085,925 bytes  Replsys.sql 01-Jun-2003 01:01  2000.80.818.0     492,096 bytes  Semobj.dll 31-May-2003 18:27  2000.80.818.0     172,032 bytes  Semobj.rll 29-May-2003 00:29                    115,944 bytes  Sp3_serv_uni.sql 01-Jun-2003 01:01  2000.80.818.0   4,215,360 bytes  Sqldmo.dll 07-Apr-2003 17:44                     25,172 bytes  Sqldumper.exe 19-Mar-2003 18:20  2000.80.789.0      28,672 bytes  Sqlevn70.rll 02-Jul-2003 00:18  2000.80.834.0     180,736 bytes  Sqlmap70.dll 08-Feb-2003 06:40  2000.80.765.0      57,920 bytes  Sqlrepss.dll 10-Jul-2003 22:00  2000.80.840.0   7,553,105 bytes  Sqlservr.exe 08-Feb-2003 06:40  2000.80.765.0      45,644 bytes  Sqlvdi.dll 25-Jun-2003 01:01  2000.80.818.0      33,340 bytes  Ssmslpcn.dll 01-Jun-2003 01:01  2000.80.818.0      82,492 bytes  Ssnetlib.dll 01-Jun-2003 01:01  2000.80.818.0      25,148 bytes  Ssnmpn70.dll 01-Jun-2003 01:01  2000.80.818.0     158,240 bytes  Svrnetcn.dll 31-May-2003 18:59  2000.80.818.0      76,416 bytes  Svrnetcn.exe 30-Apr-2003 23:52  2000.80.816.0      45,132 bytes  Ums.dll 02-Jul-2003 00:19  2000.80.834.0      98,816 bytes  Xpweb70.dll Note Because of file dependencies, the most recent hotfix or feature that contains the files may also contain additional files.



WORKAROUND
To work around the problem, use any of the following methods:
 * Disable query parallelism for the query by adding the OPTION (MAXDOP 1) query hint. This will produce the same result as the bug fix. The bug fix detects whether the MAXDOP option is set to 1, and automatically disables parallelism for this type of query.
 * Store the parameters of the stored procedure in local variables, and pass the local variables to the query as parameters.
 * Force a different join strategy, such as a loop join or a merge join.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.This problem was first corrected in Microsoft SQL Server 2000 Service Pack 4.



MORE INFORMATION
For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates

Keywords: kbbug kbfix kbstoredproc kbqfe kbsqlserv2000presp4fix kbquery KB822033

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.