February 19, 2013 at 12:49 pm
Hi,
I have a table, and I have enabled an Instead of Trigger and for Insert Trigger, the question is:
Has Instead of trigger preference over the for insert trigger?
Based on my understanding the instead of will override the for insert trigger.
Thanks
February 19, 2013 at 1:05 pm
In general, I think the preference is to avoid triggers when possible.
after that, it's not a preference, but rather which method, a standard insert trigger vs an instead of trigger addresses the specific issue best.
I've seen lots and lots of unnecessary triggers, so the classic "it depends" answer applies for each situation.
a classic example is where someone wants to update some column in the same table to be a status, based on another column.(ie if the ShippedDate is not null, that the ShippedStatus = "shipped"
something like that can easily be replaced with a view or a calculated column...no trigger really required.
Other situations, where it's an auditing type thing to an audit whodunnit table, a classic Insert/Update/Delete trigger is fine.
For me, an instead of trigger is ideal for an updatable view, that is composed of multiple tables that the view ordinarily would not support updating for.
Lowell
February 19, 2013 at 1:09 pm
Thanks, if I have both triggers enabled on the same table are they both going to fire ?
Thanks again
February 19, 2013 at 1:23 pm
yes. both triggers will fire. two triggers same table, even if one is an instead of trigger.
That might be a logical error, having two insert triggers on the same table;
an instead of trigger does not replace ALL other triggers...it just replaces the logic that would have normally been performed inside the one trigger with alternate workflow or something.
Lowell
February 19, 2013 at 5:21 pm
The "instead of" trigger will fire first.
If the "instead of" trigger does actually insert a row(s), then the "after[for] trigger" will fire.
You may want to use the clearer "AFTER INSERT" instead of "FOR INSERT" (which is obsolete now really) when creating the other trigger to help you remember the firing order :-).
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply