Microsoft KB Archive/810920

= FIX: The JOIN queries in the triggers that involve the inserted table or the deleted table may return results that are not consistent =

Article ID: 810920

Article Last Modified on 11/2/2007

-

APPLIES TO


 * Microsoft SQL Server 2000 Developer Edition
 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft SQL Server 2000 Enterprise Edition
 * Microsoft SQL Server 2000 Personal Edition
 * Microsoft SQL Server 2000 Enterprise Edition
 * Microsoft SQL Server 2000 Workgroup Edition
 * Microsoft SQL Server 2000 Desktop Engine (Windows)
 * Microsoft SQL Server 2000 Developer Edition
 * Microsoft SQL Server 2000 Enterprise Edition 64-bit

-



Bug #: 363156 (SQL Server 8.0)



SYMPTOMS
If you implement a cascading delete action between a parent table and a child table, and you define a DELETE trigger or an INSERT trigger on the child table, the join queries in the trigger may return results that are not consistent. This problem occurs when the join queries in the trigger involve the inserted table or the deleted table. For example, if the DELETE trigger on the child table includes the following queries, the results do not match: SELECT * FROM deleted INNER JOIN ParentTable on deleted.ParentID = ParentTable.ParentID SELECT COUNT(*) FROM deleted INNER JOIN ParentTable on deleted.ParentID = ParentTable.ParentID



Service pack information
To resolve this problem, obtain the latest service pack for Microsoft SQL Server 2000. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

290211 How to obtain the latest SQL Server 2000 service pack

Hotfix information
The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in coordinated universal time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.   Date         Time   Version        Size    File name --  18-Jan-2002  04:45  2000.80.568.0      29,244  Dbmslpcn.dll 30-Aug-2002 21:08                    786,432  Distmdl.ldf 30-Aug-2002 21:08                  2,359,296  Distmdl.mdf 09-Oct-2002 00:54                        544  Drop_repl_hotfix.sql 02-Jul-2002 15:35  2000.80.650.0     107,088  Impprov.dll 19-Jul-2002 16:56                    774,516  Instdist.sql 08-Oct-2002 23:56                     12,615  Inst_repl_hotfix.sql 20-Aug-2002 16:24  2000.80.679.0     111,172  Logread.exe 07-Apr-2002 02:08  2000.80.606.0      62,024  Odsole70.dll 03-Jan-2002 01:59                     18,185  Qfe356326.sql 20-Aug-2002 16:47  2000.80.679.0     135,748  Qrdrsvc.exe 26-Aug-2002 22:49  2000.80.679.0     406,088  Rdistcom.dll 04-Oct-2001 01:36                    437,302  Replcom.sql 20-Aug-2002 16:33  2000.80.679.0     152,136  Replmerg.exe 19-Nov-2001 20:36                    993,945  Replmerg.sql 05-Nov-2002 23:48  2000.80.700.0     221,768  Replprov.dll 04-Oct-2001 01:36                    986,906  Replsys.sql 04-Oct-2001 01:36                    881,228  Repltran.sql 26-Aug-2002 22:49  2000.80.679.0     283,208  Rinitcom.dll 16-Sep-2002 22:31                    390,045  Securityhotfix.sql 26-Aug-2002 22:49  2000.80.679.0      28,672  Sqlagent.dll 26-Aug-2002 18:39  2000.80.679.0     311,872  Sqlagent.exe 28-Aug-2002 16:32  2000.80.679.0      49,152  Sqlagent.rll 26-Aug-2002 22:49  2000.80.679.0      53,824  Sqlcmdss.dll 28-Aug-2002 16:40  2000.80.679.0      12,288  Sqlcmdss.rll 24-Oct-2002 17:37  2000.80.695.0     176,696  Sqlmap70.dll 21-Nov-2002 00:54  2000.80.705.0   7,471,185  Sqlservr.exe 21-Nov-2002 00:53                 12,633,088  Sqlservr.pdb 01-Nov-2002 18:56  2000.80.698.0      45,644  Sqlvdi.dll 18-Jan-2002 04:45  2000.80.568.0      29,244  Ssmslpcn.dll 01-Nov-2002 18:00  2000.80.697.0      82,492  Ssnetlib.dll 07-Apr-2002 02:08  2000.80.606.0      70,208  Xplog70.dll 07-Apr-2002 02:08  2000.80.606.0      53,828  Xpqueue.dll 07-Apr-2002 02:08  2000.80.606.0     156,228  Xprepl.dll 12-Jul-2002 01:00  2000.80.658.0     279,104  Xpstar.dll 16-Sep-2002 23:12  2000.80.686.0      98,872  Xpweb70.dll Note Because of file dependencies, the most recent hotfix or feature that contains these files may also contain additional files.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.This problem was first corrected in Microsoft SQL Server 2000 Service Pack 4.



Steps to reproduce the problem
To reproduce the problem, follow these steps:  In your database, create two tables that are named ParentTable and ChildTable.  Create a parent-child relationship between the tables that has a cascading delete reference, and then populate the tables with sample data. To do this, run the following Transact-SQL script in SQL Query Analyzer: USE  GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_ParentID]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1) ALTER TABLE [dbo].[ChildTable] DROP CONSTRAINT FK_ParentID GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t_ChildTable_Delete]') and OBJECTPROPERTY(id, N'IsTrigger') = 1) drop trigger [dbo].[t_ChildTable_Delete] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ChildTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[ChildTable] GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ParentTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[ParentTable] GO

CREATE TABLE [dbo].[ChildTable] ( [ChildID] [int] IDENTITY (1, 1) NOT NULL, [ParentID] [int] NOT NULL ) ON [PRIMARY] GO

CREATE TABLE [dbo].[ParentTable] ( [ParentID] [int] IDENTITY (1, 1) NOT NULL ) ON [PRIMARY] GO

ALTER TABLE [dbo].[ChildTable] WITH NOCHECK ADD CONSTRAINT [PK_ChildTable] PRIMARY KEY CLUSTERED ( [ChildID] ) ON [PRIMARY] GO

ALTER TABLE [dbo].[ParentTable] WITH NOCHECK ADD CONSTRAINT [pk_Parent] PRIMARY KEY CLUSTERED ( [ParentID] ) ON [PRIMARY] GO

ALTER TABLE [dbo].[ChildTable] ADD CONSTRAINT [FK_ParentID] FOREIGN KEY ( [ParentID] ) REFERENCES [dbo].[ParentTable] ( [ParentID] ) ON DELETE CASCADE GO

SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

SET IDENTITY_INSERT ParentTable ON GO INSERT INTO ParentTable (ParentID) VALUES (1) INSERT INTO ParentTable (ParentID) VALUES (2) GO SET IDENTITY_INSERT ParentTable OFF GO INSERT INTO ChildTable (ParentID) VALUES (1) INSERT INTO ChildTable (ParentID) VALUES (2) GO

SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO   Create a DELETE trigger on the child table. To do this, run the following Transact-SQL script in SQL Query Analyzer: CREATE TRIGGER t_ChildTable_Delete ON ChildTable FOR DELETE AS SELECT * FROM deleted INNER JOIN ParentTable on deleted.ParentID = ParentTable.ParentID SELECT COUNT(*) FROM deleted INNER JOIN ParentTable on deleted.ParentID = ParentTable.ParentID IF EXISTS(SELECT * FROM deleted INNER JOIN ParentTable on deleted.ParentID = ParentTable.ParentID) PRINT 'Exists returned true' ELSE PRINT 'Exists returned false'

RETURN GO   Delete data from the ParentTable table: DELETE FROM parenttable WHERE parentid=2 </ol>

Note Even thought the queries in the DELETE trigger use the same search condition, the results do not match.

<div class="references_section">