Read + Write + Report
Home | Start a blog | About Orble | FAQ | Blogs | Writers | My Orble | Login

Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)

March 24th 2009 14:54
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)

Problem
I am trying to resolve an issue with triggers. We use them for things like automatically setting the update date and time on some tables. Recently we added a second trigger to a table and now we are getting the error "Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)". For the time being we dropped that latest trigger. Can you help us to resolve this?

Solution
As a first step let's review an example which will duplicate your problem. We'll create a single table with two triggers. We will use the following table to test our triggers:


CREATE TABLE dbo.tbl_TriggerTest
(
pk_TriggerTest INT IDENTITY
, Comment NVARCHAR(256)
, TriggerSample NVARCHAR(256)
, Created DATETIME DEFAULT GETDATE()
, Updated DATETIME DEFAULT GETDATE()
)


Now let's create a trigger that sets the Updated column in our table to the current date and time by using the GETDATE() function:

CREATE TRIGGER dbo.tr_TriggerTest_Footprint
ON dbo.tbl_TriggerTest
AFTER UPDATE
AS
UPDATE dbo.tbl_TriggerTest
SET Updated = GETDATE()
FROM dbo.tbl_TriggerTest t
INNER JOIN inserted i ON t.pk_TriggerTest = i.pk_TriggerTest


The above trigger will execute whenever there is an update to the tbl_TriggerTest table. The trigger references the inserted table which is a pseudo-table available to a trigger. An update is really handled as a delete followed by an insert. There is also a deleted pseudo table that is available to a trigger. The deleted table contains the rows that were updated with their values before the update; the inserted table contains the rows that were updated with their new values.


To test the trigger, we will run the following script to insert a row into our sample table then perform an update:

INSERT dbo.tbl_TriggerTest (Comment)
VALUES ('X')
UPDATE dbo.tbl_TriggerTest
SET Comment= 'This is a comment'


The above script runs successfully and the trigger updates the Comment column value to 'This is a comment'.

Now we will create another trigger on our table to reproduce the trigger error:

CREATE TRIGGER dbo.tr_TriggerTest_Sample
ON dbo.tbl_TriggerTest
AFTER UPDATE
AS
UPDATE dbo.tbl_TriggerTest
SET TriggerSample = 'updated ' CAST(GETDATE() AS NVARCHAR(20))
FROM dbo.tbl_TriggerTest t
INNER JOIN inserted i ON t.pk_TriggerTest = i.pk_TriggerTest


This trigger updates the TriggerSample column in our table. When we rerun the test script above we get the following error message:

Msg 217, Level 16, State 1, Procedure tr_TriggerTest_Sample, Line 5
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).


Notice that both triggers are updating the table that causes the triggers to fire. Essentially what is happening is the update statements in the trigger are causing the triggers to fire again and this keeps going until the nesting level is exceeded. The resolution to this problem is to check the nesting level in the trigger and only execute the update statement one time. To do this you can use the function TRIGGER_NESTLEVEL.

Alter both of the triggers as follows:

ALTER TRIGGER dbo.tr_TriggerTest_Footprint
ON dbo.tbl_TriggerTest
AFTER UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN

UPDATE dbo.tbl_TriggerTest
SET Updated = GETDATE()
FROM dbo.tbl_TriggerTest t
INNER JOIN inserted i ON t.pk_TriggerTest = i.pk_TriggerTest
END
GO

ALTER TRIGGER dbo.tr_TriggerTest_Sample
ON dbo.tbl_TriggerTest
AFTER UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN

UPDATE dbo.tbl_TriggerTest
SET TriggerSample = 'updated ' CAST(GETDATE() AS NVARCHAR(20))
FROM dbo.tbl_TriggerTest t
INNER JOIN inserted i ON t.pk_TriggerTest = i.pk_TriggerTest
END
GO


If you run the above test script again, you will see that the error is now resolved. The change to the above triggers is to test the nesting level and only execute the code in the trigger when the nesting level is 1. The nesting level will be 1 the first time the trigger is called as a result of an update statement. Each successive time the trigger is called as a result of the same update statement, the nesting level will increment by 1.

There is another way to resolve this problem. The SQL Server configuration option "nested triggers" determines whether an action performed in a trigger can cause the trigger to fire again. The default value for "nested triggers" is 1, which allows the behavior we saw in our sample. You could change "nested triggers" to zero and disable trigger nesting.



61
Vote
Add To: del.icio.us Digg Furl Spurl.net StumbleUpon Yahoo


   

   

   


Add A Comment

To create a fully formatted comment please click here.


CLICK HERE TO LOGIN | CLICK HERE TO REGISTER

Name or Orble Tag
Home Page (optional)
Comments
Bold Italic Underline Strikethrough Separator Left Center Right Separator Quote Insert Link Insert Email
Notify me of replies
Notify extra people about this comment
Is this a private comment?
List the Email Addresses or Orble Tags of the people you would like to be notified about this comment


One per line max of 30

List the Email Addresses or Orble Tags of the people you would like to be notified about this private comment thread. Only the people in this list will be able to see or reply to your comment.


One per line max of 30

Your Name
(for the email going out to the above list, it can be different to your Orble Tag)
Your Email Address
(optional)
(required for reply notification)
Submit
More Posts
4 Posts
11 Posts
1 Posts
77 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:
0

Siddharth sood's Blogs

37 Vote(s)
0 Comment(s)
1 Post(s)
84 Vote(s)
0 Comment(s)
2 Post(s)
37 Vote(s)
0 Comment(s)
1 Post(s)
50 Vote(s)
0 Comment(s)
1 Post(s)
29 Vote(s)
0 Comment(s)
1 Post(s)
76 Vote(s)
0 Comment(s)
2 Post(s)
169 Vote(s)
0 Comment(s)
4 Post(s)
213 Vote(s)
2 Comment(s)
5 Post(s)
26 Vote(s)
0 Comment(s)
1 Post(s)
25 Vote(s)
0 Comment(s)
1 Post(s)
Moderated by Siddharth sood
Copyright © 2006 2007 2008 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]