FOR 和 AFTER 触发器之间的区别?

发布于 2024-10-23 14:31:19 字数 56 浏览 1 评论 0原文

FORAFTER 触发器之间有什么区别?

What's the difference between FOR and AFTER triggers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

昔梦 2024-10-30 14:31:19

没有什么区别,他们做同样的事情。

CREATE TRIGGER trgTable on dbo.Table FOR INSERT,UPDATE,DELETE

CREATE TRIGGER trgTable on dbo.Table AFTER INSERT,UPDATE,DELETE

INSTEAD OF 触发器不同,它在插入之前触发,而不是插入,并且可以在视图上使用,以便将适当的值插入到基础表中。

There is no difference, they do the same thing.

CREATE TRIGGER trgTable on dbo.Table FOR INSERT,UPDATE,DELETE

Is the same as

CREATE TRIGGER trgTable on dbo.Table AFTER INSERT,UPDATE,DELETE

An INSTEAD OF trigger is different, and fires before and instead of the insert and can be used on views, in order to insert the appropriate values into the underlying tables.

窝囊感情。 2024-10-30 14:31:19

@Ben 绝对正确。

这是MSDN文章 探索SQL Server 触发器

文章中的一段话:

该语法在旧版本的 SQL Server 中也是可以接受的。然而,现在 SQL Server 2000 中有两种类型的触发器,我更喜欢将 FOR 触发器称为 AFTER 触发器。因此,在本文的其余部分中,我将参考 AFTER 或 INSTEAD OF 触发器。

与您之前看到的 AFTER 触发器一样,此触发器可防止对姓氏字段进行更改。但是,它实现此业务规则的方式与前面的示例不同。由于 INSTEAD OF 触发器代替 UPDATE 语句触发,因此 INSTEAD OF 触发器随后将评估业务规则测试是否通过。如果业务规则测试通过,为了进行更新,INSTEAD OF 触发器必须再次显式调用 UPDATE 语句。

@Ben is absolutely right.

Here is MSDN article Exploring SQL Server Triggers

A paragraph from the article:

That syntax is also acceptable in older versions of SQL Server. However, now that there are two types of triggers in SQL Server 2000, I prefer to refer to FOR triggers as AFTER triggers. Thus, for the remainder of this article I will refer to either AFTER or INSTEAD OF triggers.

Like the AFTER trigger you saw earlier, this trigger prevents changes from being made to the lastname field. However, it implements this business rule differently than the previous example. Because the INSTEAD OF trigger fires in place of the UPDATE statement, the INSTEAD OF trigger then evaluates if the business rule test passes or not. If the business rule test passes, in order for the update to occur the INSTEAD OF trigger must explicitly invoke the UPDATE statement again.

究竟谁懂我的在乎 2024-10-30 14:31:19

AFTER 指定仅当触发 SQL 语句中指定的所有操作均已成功执行时才触发 DML 触发器。在此触发器触发之前,所有引用级联操作和约束检查也必须成功。
当 FOR 是唯一指定的关键字时,AFTER 是默认值。

不能在视图上定义 AFTER 触发器。

代替
指定执行 DML 触发器而不是触发 SQL 语句,因此覆盖触发语句的操作。不能为 DDL 或登录触发器指定 INSTEAD OF。

https://learn.microsoft.com /en-us/sql/t-sql/statements/create-trigger-transact-sql

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.

INSTEAD OF
Specifies that the DML trigger is executed instead of the triggering SQL statement, therefore, overriding the actions of the triggering statements. INSTEAD OF cannot be specified for DDL or logon triggers.

https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql

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