SQL 触发器中的 FOR/AFTER
我是 SQL 新手。我正在阅读有关 SQL 中触发器的内容。我已经了解了几乎有关触发器的内容。但在DML触发器中,我们使用FOR/AFTER关键字。我没有明白 FOR/AFTER 之间的区别以及为什么我们使用 FOR/AFTER 关键字。我已经在 MSDN 上阅读过,但没有得到简单的答案。 谁能解释一下这是什么?
提前致谢。
I am newbie in SQL. I am reading about Triggers in SQL.I have got almost about Triggers. But in DML Triggers, we use FOR/AFTER keyword. I didn't get difference between FOR/AFTER and why we use FOR/AFTER keyword. I have already read on MSDN but didn't get the simple answer.
Can anyone explain me what is it?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 FOR 和 AFTER 之间没有区别。
我相信原始(2000 年之前)语法仅使用了 FOR 关键字。但是,当引入 INSTEAD OF 触发器时,“FOR " 关键字可能看起来很混乱。 “AFTER”更准确地传达了触发器的类型,并且更容易与“INSTEAD OF”区分开。
如果我们想要转换插入到表中的内容,或者阻止插入发生,则可以使用 INSTEAD OF 触发器。
如果我们想根据刚刚发生的事情执行其他任务,通常会使用 AFTER 触发器。例如,您可以有一个“AFTER DELETE”触发器,它将删除的行复制到某种存档表中。基本上,在 AFTER 触发器中,您通常仍然希望该活动发生。
There is no difference between using FOR and AFTER.
I believe the original (pre 2000) syntax only used the FOR keyword. However, when INSTEAD OF triggers were introduced, the "FOR" keyword could seem quite confusing. "AFTER" more accurately conveys the type of trigger, and is more easily distinguished from "INSTEAD OF".
An INSTEAD OF trigger would be used if we wanted to transform what was inserted into the table, or prevent an insertion from taking place.
An AFTER trigger would more normally be used if we wanted to perform additional tasks, based on what has just occurred. For instance, you could have an "AFTER DELETE" trigger, that copied deleted rows into some kind of archive table. Basically, in an AFTER trigger, you more normally do still want the activity to occur.
来自 MSDN:
如果违反约束,则永远不会执行 AFTER 触发器发生;因此,这些触发器不能用于任何可能防止违反约束的处理。
并且then:
您可以通过指定 AFTER 或 FOR 关键字来请求 AFTER 触发器。因为FOR关键字和AFTER的作用是一样的,所以带有FOR关键字的DML触发器也被归类为AFTER触发器
看起来没有什么区别。
From MSDN:
AFTER triggers are never executed if a constraint violation occurs; therefore, these triggers cannot be used for any processing that might prevent constraint violations.
And then:
You can request AFTER triggers by specifying either the AFTER or FOR keywords. Because the FOR keyword has the same effect as AFTER, DML triggers with the FOR keyword are also classified as AFTER triggers
It would seem there is no difference.
如果我正确地解释您对其他答案的评论,您想知道为什么或何时使用“FOR | AFTER”关键字。
很简单:有两种触发器,AFTER 触发器和 INSTEAD-OF 触发器。
例如,插入操作的 INSTEAD-OF-trigger 可以写为
,而 AFTER-trigger 可以写为
或
正如 Damien_The_Unknowner 提到的,AFTER 关键字比 FOR 版本更具可读性,仅此而已。
If I interpret your comments to the other answers correctly, you want to know why or when one uses the "FOR|AFTER" keywords.
It's simple: there are two kinds of triggers, the AFTER-trigger and the INSTEAD-OF-trigger.
The INSTEAD-OF-trigger for e.g. an insert action can be written as
and the AFTER-trigger can be written as either
or
As Damien_The_Unbeliever mentions, the AFTER keyword is more readable than the FOR version, that is all.
他们是一样的。请参阅 BOL 的摘录
”
为 |后
AFTER 指定仅当触发 SQL 语句中指定的所有操作均已成功执行时才触发 DML 触发器。在此触发器触发之前,所有引用级联操作和约束检查也必须成功。
当 FOR 是唯一指定的关键字时,AFTER 是默认值。
不能在视图上定义 AFTER 触发器。
”
They are the same. See this excerpt from BOL
"
FOR | AFTER
AFTER specifies that the DML trigger is fired only when all operations specified in the triggering SQL statement have executed successfully. All referential cascade actions and constraint checks also must succeed before this trigger fires.
AFTER is the default when FOR is the only keyword specified.
AFTER triggers cannot be defined on views.
"
根据我的观察,FOR用于DDL触发器,而AFTER用于DML触发器。他们有相同的工作方式。
According to what I observe, FOR is used in DDL trigger while AFTER is used in DML triggers. They have same way of working.