Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Circular Reference

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-01-11 : 08:47:02
Ray writes "How do I prevent a circular reference in an insert trigger? My table can be reduced to :

ID - int identity primary key
Name - varchar
ParentID - int foreign key to ID

How do I write an insert trigger to prevent the following situation:

ID=3 has ParentID=2 and
ID=2 has ParentID=1 and
ID=1 has ParentID=3

I can't figure out how to write the insert trigger."

jhermiz

3564 Posts

Posted - 2005-01-11 : 11:59:12
WHERE ID <> 3 AND ParentID <> 2 ... ?


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]


Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-01-12 : 05:15:33
think of it logically
.....you should be inserting ID 1 first....what parent does it get?...that parent should exist at that stage...if it doesn't the insert fails.

you do have issues if you are restructuring the hierarchy after it has been created....but that should be resolved by identifying if the new parent is any child of the existing structure.

Search here for "Hierarchies" for some articles/discussion points on this topic.
Go to Top of Page
   

- Advertisement -