dbTalk Databases Forums  

SQL Server 2008: Trigger Question

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL Server 2008: Trigger Question in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gene Wirchenko
 
Posts: n/a

Default SQL Server 2008: Trigger Question - 03-01-2011 , 06:55 PM






Dear SQLers:

CREATE TRIGGER ... {FOR | AFTER | INSTEAD OF} ...
Do FOR and AFTER mean the same thing here? In particular, I am
wondering whether a FOR trigger executes before or after the
underlying data is modified. My text says before on one page and
after on the next, and MSDN is not clear either.

Sincerely,

Gene Wirchenko

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL Server 2008: Trigger Question - 03-02-2011 , 01:51 AM






Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Quote:
CREATE TRIGGER ... {FOR | AFTER | INSTEAD OF} ...
Do FOR and AFTER mean the same thing here? In particular, I am
wondering whether a FOR trigger executes before or after the
underlying data is modified. My text says before on one page and
after on the next, and MSDN is not clear either.
FOR and AFTER are indeeed equivalent. Originally FOR was the only keyword.
AFTER was added when they introduced INSTEAD OF.

Notice that there are no BEFORE triggers in SQL Server, only INSTEAD OF.
(In a real BEFORE trigger, you don't need to redo the command.)


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #3  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: SQL Server 2008: Trigger Question - 03-02-2011 , 02:51 PM



On Wed, 2 Mar 2011 07:51:01 +0000 (UTC), Erland Sommarskog
<esquel (AT) sommarskog (DOT) se> wrote:

Quote:
Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
CREATE TRIGGER ... {FOR | AFTER | INSTEAD OF} ...
Do FOR and AFTER mean the same thing here? In particular, I am
wondering whether a FOR trigger executes before or after the
underlying data is modified. My text says before on one page and
after on the next, and MSDN is not clear either.

FOR and AFTER are indeeed equivalent. Originally FOR was the only keyword.
AFTER was added when they introduced INSTEAD OF.

Notice that there are no BEFORE triggers in SQL Server, only INSTEAD OF.
(In a real BEFORE trigger, you don't need to redo the command.)
Thank you. And what about when the underlying data gets
modified?

Sincerely,

Gene Wirchenko

Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL Server 2008: Trigger Question - 03-02-2011 , 08:27 PM



Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Quote:
Thank you. And what about when the underlying data gets
modified?
An INSTEAD OF triggers fires instead of the INSERT/UPDATE/DELETE/MERGE
statment that fired the trigger, and you need to resubmit the statement
if you want to carry out the modification.

FOR and AFTER triggers fires after the modification has taken place, and
constraints have been validated. By default, if there is an error in the
trigger, this rolls back the entire statement, even if there was no explicit
transaction.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #5  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: SQL Server 2008: Trigger Question - 03-02-2011 , 08:58 PM



On Thu, 3 Mar 2011 02:27:37 +0000 (UTC), Erland Sommarskog
<esquel (AT) sommarskog (DOT) se> wrote:

Quote:
Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Thank you. And what about when the underlying data gets
modified?

An INSTEAD OF triggers fires instead of the INSERT/UPDATE/DELETE/MERGE
statment that fired the trigger, and you need to resubmit the statement
if you want to carry out the modification.

FOR and AFTER triggers fires after the modification has taken place, and
constraints have been validated. By default, if there is an error in the
trigger, this rolls back the entire statement, even if there was no explicit
transaction.
If I understand you correctly, then this covers my main concern:
atomicity of the whole operation.

Thank you.

Sincerely,

Gene Wirchenko

Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL Server 2008: Trigger Question - 03-03-2011 , 09:22 AM



Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Quote:
If I understand you correctly, then this covers my main concern:
atomicity of the whole operation.
Yes, that is what you get.

If you for some reason don't want the atomicity, there are ways to cheat.
And, yes, I've seen people to ask for that.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #7  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: SQL Server 2008: Trigger Question - 03-03-2011 , 11:30 AM



On Thu, 3 Mar 2011 15:22:10 +0000 (UTC), Erland Sommarskog
<esquel (AT) sommarskog (DOT) se> wrote:

Quote:
Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
If I understand you correctly, then this covers my main concern:
atomicity of the whole operation.

Yes, that is what you get.
Good. Thank you.

Quote:
If you for some reason don't want the atomicity, there are ways to cheat.
And, yes, I've seen people to ask for that.
Do they talk about their need to be flexible? Even brag about it
a bit?

I am near the end of my beginner's text. I keep hearing about
isolation levels, expect the cheating involves them, and rather doubt
my text covers it.

Sincerely,

Gene Wirchenko

Reply With Quote
  #8  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL Server 2008: Trigger Question - 03-04-2011 , 11:16 AM



Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Quote:
Do they talk about their need to be flexible? Even brag about it
a bit?
I don't knwo exactly what they are up to. But it seems in some cases
they are poking in third-party applications, and don't want to disrupt
the flow, since they are doing something their vendor does not support.

I have also seen things about this in conjunction with replication. You
may have a trigger on the subscriber for whatever reason, but you don't
want this trigger to stop the entire replication going.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.