Microsoft KB Archive/303218

= FIX: Distribution Agent Fails when Applying Snapshot if the Table has a Self-referencing Foreign Key =

Article ID: 303218

Article Last Modified on 10/3/2003

-

APPLIES TO


 * Microsoft SQL Server 2000 Standard Edition

-



This article was previously published under Q303218



BUG #: 352248 (SHILOH_BUGS)



SYMPTOMS
If a transactional publication has a table with a self-referencing foreign key, and the table article is configured to include declared referential integrity in a snapshot, the Distribution Agent fails while trying to reapply the snapshot after the first synchronization. The Distribution Agent fails with this error message:

Unable to drop table [Table Name], because it is accessed by a Foreign Key constraint.



CAUSE
When applying the snapshot, the schema files are applied in such a way that the child tables are dropped, and then re-created before the parent tables are re-created. So, even if there are foreign key references, tables can be dropped and re-created successfully. However, if there is a self-referencing foreign key, you must remove the foreign key before you can drop the table. When the Distribution Agent tries to apply the schema of a table that has a self-referencing foreign key the second time, the agent fails with the error message shown in the &quot;Symptoms&quot; section.

This problem occurs if the pre_creation_command option for the snapshot is Drop the existing table and recreate it, and the table article is configured to include declared referential integrity in the snapshot.



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

290211 INF: How to Obtain the Latest SQL Server 2000 Service Pack



WORKAROUND

 * 1) Drop the self-referencing key manually before you apply the snapshot the second time.
 * 2) Use the pre-snapshot script to drop all foreign key references.



STATUS
Microsoft has confirmed that this is a problem in SQL Server 2000. This problem was first corrected in SQL Server 2000 Service Pack 1.



Steps to Reproduce Behavior
  Create two tables. In one table create a self-referencing key.

For example: Create table Bugtest ( Col1 Int Primary Key, Col2 int Foreign Key references Bugtest(Col1) )

Create table Bugtest1 ( Col1 Int Primary Key Foreign Key references Bugtest(Col1), Col2 int  )  Create a transactional publication with the two tables you created in step 1 as articles. Navigate to the Table Article Properties/Snapshot, and enable Include declared referential integrity for each article. Create a subscription, and then apply the snapshot. This works fine.</li> Reinitialize the subscription, re-run the Snapshot Agent, and then run the Distribution Agent to apply the snapshot.</li></ol>

The Distribution Agent fails with this error message:

&quot;Could not drop object 'Bugtest' because it is referenced by a FOREIGN KEY constraint.&quot;

If you remove the self-referencing foreign key, the Distribution Agent works fine, even though there is another foreign key that references the table BugTest.

Additional query words: snapshot foriegn key self referencing drop

Keywords: kbbug kbfix kbsqlserv2000sp1fix KB303218

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.