BUG: IF Statement with Two EXISTS Separated by OR Clause

Last reviewed: April 28, 1997
Article ID: Q106133

The information in this article applies to:
  • Microsoft SQL Server version 4.2 for OS/2
  • Microsoft SQL Server, version 4.2
BUG# OS/2: 1795 (4.2)
       NT:  544 (4.2)

SYMPTOMS

If one of the SELECT statements is against an empty tableA query that issues an IF EXISTS statement with an OR clause can process incorrectly. The basic logic looks like:

   If exists (SELECT f1 FROM table1 WHERE f1 = "z") OR
      exists (SELECT f1 FROM table2 WHERE f1 = "a")
         select "TRUE"
   else
         select "FALSE"

If table1 is empty and table2 has a row that matches the select, the statement should return TRUE; however, it returns FALSE. If a row is added to table1, even though it does not meet the search criteria, the IF EXISTS statement will process correctly.

CAUSE

SQL Server is not correctly evaluating the IF statement when one or more of the tables has no rows.

WORKAROUND

This problem can be avoided by making sure that both tables contain at least one row. An alternate method is to restructure the IF statement to evaluate one of the EXISTS at a time. In the example given above, it would look like the following:

   If exists (SELECT f1 FROM table1 WHERE f1 = "z")
      select "TRUE"
   else if exists (SELECT f1 FROM table2 WHERE f1 = "a")
      select "TRUE"
   else
      select "FALSE"

STATUS

Microsoft has confirmed this to be a problem in SQL Server version 4.2 for OS/2 and Microsoft SQL Server version 4.2. 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 Windows NT
Keywords : kbbug4.20 kbprg SSrvServer SSrvWinNT
Version : 4.2 | 4.2 4.2a
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.