SQL 触发器中的 FOR/AFTER

发布于 2024-10-03 02:35:03 字数 178 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

铃予 2024-10-10 02:35:03

使用 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.

也只是曾经 2024-10-10 02:35:03

来自 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.

我为君王 2024-10-10 02:35:03

如果我正确地解释您对其他答案的评论,您想知道为什么或何时使用“FOR | AFTER”关键字。

很简单:有两种触发器,AFTER 触发器和 INSTEAD-OF 触发器。
例如,插入操作的 INSTEAD-OF-trigger 可以写为

create trigger myTrigger on myTable
INSTEAD OF insert
begin
    (... code goes here ...)
end

,而 AFTER-trigger 可以写为

create trigger myTrigger on myTable
AFTER insert
begin
    (... code goes here ...)
end

create trigger myTrigger on myTable
FOR insert
begin
    (... code goes here ...)
end

正如 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

create trigger myTrigger on myTable
INSTEAD OF insert
begin
    (... code goes here ...)
end

and the AFTER-trigger can be written as either

create trigger myTrigger on myTable
AFTER insert
begin
    (... code goes here ...)
end

or

create trigger myTrigger on myTable
FOR insert
begin
    (... code goes here ...)
end

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.
"

や莫失莫忘 2024-10-10 02:35:03

根据我的观察,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文