BUG: Update Using Subquery with Aggregate Functions

Last reviewed: April 28, 1997
Article ID: Q87311

The information in this article applies to:
  • Microsoft SQL Server version 4.2 for OS/2
  • Microsoft SQL Server, versions 4.2, 4.21, and 4.21a
BUG# OS/2: 1436 (4.2)
       NT:  880 (4.2)

SYMPTOMS

In an update statement, if you use a subquery to set the value of a column and include one or more aggregate functions with arithmetic operations, an error occurs complaining that the subquery returns multiple values, even if it does not.

For example, the following causes error 512:

   Update t1 set c2 =(select sum(c)*1
      from t2
      where t1.c1=t2.c)

However, the following query, without the arithmetic operation (*1), runs without error:

   Update t1 set c2 =(select sum(c)
      from t2
      where t1.c1=t2.c)

CAUSE

SQL Server incorrectly handles arithmetic operations in a subquery that returns aggregate results if the subquery updates a table.

WORKAROUND

One workaround is to move the constant inside the parentheses if the query remains semantically correct. For example, you can rewrite sum(c)*2 as sum(c*2). Otherwise you can break up the query and use intermediate tables to do the updates.

STATUS

Microsoft has confirmed this to be a problem in SQL Server 4.2 for OS/2 and Microsoft SQL Server versions 4.20, 4.21, and 4.21a. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.


Additional query words: Transact-SQL
Keywords : kbbug4.20 kbbug4.21 kbbug4.21a kbprg SSrvServer SSrvWinNT
Version : 4.2 | 4.2 4.21 4.21a
Platform : OS/2 WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.