Solved

Recursive Trigger example needed in SQL Server !

Posted on 2009-07-16
6
541 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

861 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