INF: Aggregates and SET Clause in UPDATE Statement

Last reviewed: April 3, 1997
Article ID: Q90477

The information in this article applies to:

  - Microsoft SQL Server version 4.2 for OS/2

SUMMARY

In an UPDATE statement, an aggregate function cannot appear directly in the SET list. For example, an attempt to execute a query such as,

   UPDATE <table name>
   SET <column name> = count(*)
   FROM <table names>
   WHERE <condition List>

generates the following message in 4.2:

   An aggregate may not appear in the set list of an UPDATE statement.
   (Msg 157, Level 15, State 1).

MORE INFORMATION

When an aggregate appears in the SET list of an UPDATE statement as above, there exists an ambiguity as to whether the associated WHERE clause qualifies the rows to be updated or it qualifies the rows on which to apply the aggregate function. The correct way to do this is:

   UPDATE <table name>
   SET <column name> = (select count(*) from <table name>
                        where <aggregate condition>)
   FROM <table names>
   WHERE <update condition>.

This is in accordance with ANSI Specification.


Additional query words: 4.20
Keywords : kberrmsg SSrvServer


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 3, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.