BUG: Bad Trigger Generated for Table with Nullable Foreign Key

Last reviewed: April 30, 1997
Article ID: Q122256

The information in this article applies to:
  • Microsoft SQL Server Programmer's Toolkit, version 4.2
BUG# 10388 (4.2)

SYMPTOMS

Incorrect SQL is generated if Object Manager (OM) is used to generate an insert trigger on a table containing a nullable foreign key.

The code generated will be similar to:

   IF( SELECT COUNT(*) FROM master, inserted WHERE inserted.id = master.id
   OR inserted.id IS NULL ) <> ( SELECT COUNT(*) FROM inserted )
   begin
       raiserror
       rollback transaction
   end

This is incorrect since the clause to the right of the OR is an open join with the master table. Thus, it will result in a count of all records in master for each row with NULL row being added.

WORKAROUND

A trigger to correctly check primary key/foreign key relationships can be created using the following syntax:

   IF (SELECT COUNT(*) FROM master, inserted WHERE inserted.id = master.id)
   <> (SELECT COUNT(*) FROM inserted WHERE inserted.id IS NOT NULL )
   BEGIN
       /* error handling code */
   END

STATUS

Microsoft has confirmed this to be a problem in SQL Object Manager 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: Windows NT
Keywords : kbbug4.20 kbprg kbtool SSrvObj_Man SSrvWinNT
Version : 4.2
Platform : 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 30, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.