开发者

how to debug a recursive trigger

I have a recursive trigger, that seems to do exactly what I want it to with no recursion, however when I turn recursion on I get the error: "Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)"

This should not happen, as I expect 2 or maybe 3 levels of nesting, so I need to debug the trigger and work out what exactly is going on. I added a print statement, but that does not work...

How do you go about debugging a recursive trigger?

ALTER TRIGGER [dbo].[DataInstance2_Trigger] 
   ON  [dbo].[DataInstance]  
AFTER UPDATE
AS
BEGIN 
  UPDATE DataInstance
  SET
    DataInstance.IsCurrent = i.IsCurrent
    FROM DataInstance di
    Inner join DataContainer dc on
        di.DataContainerId = dc.DataContainerId
    Inner join Inserted i on
        dc.ParentDataContainerId = i.DataContainerId    
    WHERE di.IsCurrent != i.IsCurrent     

    declare @x int
    SET @X =开发者_开发问答 (select max(DataContainerId) from Inserted)
    print @X

END


You can include a SELECT statement in your trigger (I've just tried this - try SELECT * FROM DataInstance prior to the UPDATE in the trigger).

I've repro'ed the problem - the UPDATE in the trigger is causing the trigger to fire again even if there are no rows updated. A suitable fix would be to wrap the UPDATE statement within the trigger in an IF (SELECT COUNT(*) FROM INSERTED) <> 0 BEGIN ... END block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜