146

What's the difference between FOR and AFTER triggers?

2
  • 8
    +1: I read the SQL Server Books Online Article on Triggers and ended up with the exact same question
    – pfunk
    Commented May 27, 2011 at 13:35
  • I found this article useful: dotnettricks.com/learn/sqlserver/…
    – Matt
    Commented Oct 20, 2016 at 8:35

3 Answers 3

191

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.

3
  • 17
    I liked the comment about inserts into views using triggers. Very useful info. Cheers.
    – Mariusz
    Commented Sep 19, 2015 at 9:45
  • Excuse Mister @Ben, What would be the scenario to insert into a view? Thanks ^-^ Commented Mar 9, 2017 at 3:45
  • 1
    @JeancarloFontalvo, 1) compatibility. The underlying tables can change provided the view remains the same. 2) access control, certain users may have permission to write to certain columns only.
    – Ben
    Commented Mar 9, 2017 at 8:31
23

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

7

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

1
  • 4
    This phrasing "AFTER is the default when FOR is the only keyword specified." is very confusing. They could have worded it better.
    – FMFF
    Commented Oct 24, 2019 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.