How do I prevent Trigger recursion in SQLite?

半世苍凉 提交于 2020-03-03 09:07:30

问题


I am trying to create a trigger which will update the grades of all the friends of a student whose grade is updated.

create trigger upgrade
after update on Highschooler
when new.grade=old.grade+1 and trigger_nestlevel() < 2
begin
    update Highschooler
    set grade=new.grade
    where ID in (
      select ID2
      from Friend
       where ID1=old.ID
    )
;
end

friends of friends (of friends...) of the person whose grade is increased are also being 'upgraded' how can i stop this?


回答1:


As you can read in Limits In SQLite you can clear the recursive trigger capability by using the PRAGMA recursive_triggers statement.

10. Maximum Depth Of Trigger Recursion

SQLite limits the depth of recursion of triggers in order to prevent a statement involving recursive triggers from using an unbounded amount of memory.

Prior to SQLite version 3.6.18, triggers were not recursive and so this limit was meaningless. Beginning with version 3.6.18, recursive triggers were supported but had to be explicitly enabled using the PRAGMA recursive_triggers statement. Beginning with version 3.7.0, recursive triggers are enabled by default but can be manually disabled using PRAGMA recursive_triggers. The SQLITE_MAX_TRIGGER_DEPTH is only meaningful if recursive triggers are enabled.

The default maximum trigger recursion depth is 1000.



来源:https://stackoverflow.com/questions/8372987/how-do-i-prevent-trigger-recursion-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!