Microsoft KB Archive/87311

{|
 * width="100%"|

BUG: Update Using Subquery with Aggregate Functions

 * }

Q87311

-

The information in this article applies to:


 * Microsoft SQL Server for OS/2, version 4.2
 * Microsoft SQL Server version 4.2x

-

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 : kbprogramming

Issue type :

Technology : kbSQLServSearch kbAudDeveloper kbZNotKeyword3 kbSQLServ420OS2