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)
 Trigger - Cascaded Delete

Author  Topic 

abarsami
Yak Posting Veteran

68 Posts

Posted - 2001-01-17 : 20:39:22
I need to create a trigger that will do a cascaded delete throughout 1 table.
Let's say 1 table has a Primary_key column. It also has a foreign_key column that refers to the primary_key column in that table.
I want to be able to delete the table by saying.

Delete table1 where primary_key = X

So it first goes and deletes any rows that have foreign keys the same as the primary key.
After it has finished all of that, then it can go ahead and delete table1 where primary_key = X.

Ex:

Table1

primary_key foreign_key
___________ ____________

1 NULL
2 1
3 1
4 3
5 2

So if I say Delete Table1 where primary_key=1

It first deletes all the rows with a foreign_key of 1, then also deletes rows 4 and 5, because they are foreigned key to columns 2 and 3. Finally it deletes row 1.

   

- Advertisement -