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
 General SQL Server Forums
 New to SQL Server Programming
 Help in delete trigger

Author  Topic 

gnobber
Starting Member

11 Posts

Posted - 2009-09-17 : 23:05:14
I have this table (Account) containing fields ID and ParentID. ParentID's value is the ID of the parent account for that account. Now, I set up a trigger that on delete of the parent, all children
also gets deleted. But the delete only works one level. I mean when I delete the parent, the direct children gets deleted but the children of those chidlren does not get deleted. Here is my trigger:

CREATE TRIGGER [Base].[Account_DeleteChildAccounts] ON [Base].[Account]
AFTER DELETE
AS
BEGIN
DELETE FROM [Base].[Account]
WHERE ParentID IN (SELECT ID FROM Deleted)
END

I thought, when the parent deletes its children via the trigger, the trigger would again fire and delete its chidlren. How would I go about doing this? And is there a difference in AFTER DELETE and FOR DELETE? Please help.

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-09-18 : 03:04:54
Look up "recursive cte" in Books Online...

- Lumbago
Go to Top of Page

gnobber
Starting Member

11 Posts

Posted - 2009-09-18 : 03:53:59
Thanks!
Go to Top of Page
   

- Advertisement -