Solved

Recursive Trigger example needed in SQL Server !

Posted on 2009-07-16
6
506 Views
Last Modified: 2012-05-07
Hi,

Can you please provide me basic example for "Recursive Trigger" in SQL Server ? Please provide me simple example with explanation in simple terms so that i can understand it easily.

Thanks
0
Comment
Question by:milani_lucie
  • 3
  • 2
6 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 260 total points
ID: 24869171
Here is a good example of one from:
http://www.sqlmag.com/Articles/ArticleID/23123/23123.html?Ad=1

Note 'nested triggers' and 'recursive triggers' are just indicating that when a trigger is fired others may be as well.  In the case of a recursive trigger it is firing itself over and over, so can think of recursive as a specific kind of nested trigger.

http://msdn.microsoft.com/en-us/library/ms190739.aspx


ALTER DATABASE <db_name> SET RECURSIVE_TRIGGERS ON

GO
 

CREATE TRIGGER trg_d_Tree ON Tree FOR DELETE

AS
 

-- this will stop execution when no more children found

IF @@rowcount = 0 RETURN
 

-- notice here that you DELETE from same table this trigger is for

-- therefore, this trigger will fire again

DELETE FROM T

FROM Tree AS T JOIN deleted AS D

  ON T.parentid = D.childid

GO

Open in new window

0
 

Author Comment

by:milani_lucie
ID: 24871798
So i have this table:

CREATE TABLE Tree
(
  childid  int NOT NULL PRIMARY KEY,
  parentid int NULL
)
GO

INSERT INTO Tree VALUES(1, NULL)
INSERT INTO Tree VALUES(2, 1)
INSERT INTO Tree VALUES(3, 1)
INSERT INTO Tree VALUES(4, 1)
INSERT INTO Tree VALUES(5, 2)
INSERT INTO Tree VALUES(6, 2)
INSERT INTO Tree VALUES(7, 5)
INSERT INTO Tree VALUES(8, 5)
GO

Now can you please explain to me if I delete the row containing childid = 2, how the rows with childids 5, 6, 7, and 8 will be deleted as well ? Please explain to me in simple terms.

Thanks
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24871938
Ok, so you start out issuing a statement like:

DELETE FROM Tree
WHERE childid = 2;

Which fires your trigger, trg_d_Tree.

With recursive trigger support, the trg_d_Tree is allowed to call itself; therefore, when the delete statement in the trigger gets executed it not only deletes records with childid 5 and 6 but also fires trigger again and deletes the children of 5 and 6 which are childid 7 and 8.  

The trigger actual will fire again; however, now there are no children of childid 7 and 8, so when the trigger is fired for the last time @@rowcount = 0 and the recursion stops.

Leaving us with 5 total rows affected.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:milani_lucie
ID: 24871995
Let us say you are deleting this record:

INSERT INTO Tree VALUES(2, 1)

-- ChildId = 2
-- ParentId = 1

DELETE FROM T FROM Tree AS T JOIN deleted AS D ON T.parentid = D.childid

This statement will give you these two rows:

INSERT INTO Tree VALUES(5, 2)
INSERT INTO Tree VALUES(6, 2)

It will delete these two rows. Now how the records

INSERT INTO Tree VALUES(7, 5)
INSERT INTO Tree VALUES(8, 5)

will also be deleted ? Do the DELETED magic table will have all the records that were deleted ? I think it will contain ONLY last record. Please correct me if i am wrong !

Thanks
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24872113
Each time the trigger is fired, the deleted table will only contain the record(s) which were just deleted.  Correct!

Therefore, you will see 1, then 2, and then another 2 records in deleted.
0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 240 total points
ID: 24878419
What mwvisa1 is saying is absolutely correct.

Each "layer" of delete is resolved through it's own "deleted" table.

You probably need to try it for yourself.


CREATE TABLE Tree 

(

  childid  int NOT NULL PRIMARY KEY,

  parentid int NULL

)

GO
 

CREATE TRIGGER trg_d_Tree ON Tree FOR DELETE

AS

IF @@rowcount = 0 RETURN

DELETE Tree FROM Tree INNER JOIN deleted AS D ON Tree.parentid = D.childid

GO
 

INSERT INTO Tree VALUES(1, NULL)

INSERT INTO Tree VALUES(2, 1)

INSERT INTO Tree VALUES(3, 1)

INSERT INTO Tree VALUES(4, 1)

INSERT INTO Tree VALUES(5, 2)

INSERT INTO Tree VALUES(6, 2)

INSERT INTO Tree VALUES(7, 5)

INSERT INTO Tree VALUES(8, 5)

GO
 

DELETE from tree where childid = 2

GO
 

SELECT * from Tree

GO
 

-- left with (1,NULL) and (3,1) and (4,1)

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now