Friday, February 24, 2012

For Insert/After Insert -- Default Trigger Question

Hello,
I am a little between For Insert and After Insert statements on
Triggers. I just need to know which is the Sql Server Default Trigger. Her
e
are 2 sample Triggers. Is For Insert or After Insert the default?
---
CREATE TRIGGER my_trig
ON my_table
FOR INSERT
AS
IF UPDATE(b)
PRINT 'Column b Modified'
GO
---
Create Trigger Update_Status
On ACRPLU AFTER INSERT
As
Update A Set A.a1updt = 'D'
From ACRPLU A
Where (A.a1updt = '' Or IsNull(A.a1updt, -1) < 0) And A.PK
In(Select Distinct PK From Inserted)
---
Thanks.
RichRich,
From BOL:
AFTER is the default, if FOR is the only keyword specified.
HTH
Jerry
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:6A02BEF5-E7A3-44DF-B6B3-6C26E336A319@.microsoft.com...
> Hello,
> I am a little between For Insert and After Insert statements on
> Triggers. I just need to know which is the Sql Server Default Trigger.
> Here
> are 2 sample Triggers. Is For Insert or After Insert the default?
> ---
> CREATE TRIGGER my_trig
> ON my_table
> FOR INSERT
> AS
> IF UPDATE(b)
> PRINT 'Column b Modified'
> GO
> ---
> Create Trigger Update_Status
> On ACRPLU AFTER INSERT
> As
> Update A Set A.a1updt = 'D'
> From ACRPLU A
> Where (A.a1updt = '' Or IsNull(A.a1updt, -1) < 0) And A.PK
> In(Select Distinct PK From Inserted)
> ---
> Thanks.
> Rich
>|||AFTER is the default. In other words "AFTER INSERT" means the same as "FOR
INSERT".
David Portas
SQL Server MVP
--
"Rich" wrote:

> Hello,
> I am a little between For Insert and After Insert statements on
> Triggers. I just need to know which is the Sql Server Default Trigger. H
ere
> are 2 sample Triggers. Is For Insert or After Insert the default?
> ---
> CREATE TRIGGER my_trig
> ON my_table
> FOR INSERT
> AS
> IF UPDATE(b)
> PRINT 'Column b Modified'
> GO
> ---
> Create Trigger Update_Status
> On ACRPLU AFTER INSERT
> As
> Update A Set A.a1updt = 'D'
> From ACRPLU A
> Where (A.a1updt = '' Or IsNull(A.a1updt, -1) < 0) And A.PK
> In(Select Distinct PK From Inserted)
> ---
> Thanks.
> Rich
>|||Thanks all for your replies. If I understand the explanations correctly,
what I am interpreting is that
CREATE TRIGGER my_trig
ON my_table
FOR INSERT
...
is the same as
CREATE TRIGGER my_trig
ON my_table
After INSERT
...
Is this correct? This is where my confusion lies.
Thanks,
Rich|||Yes.
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:E21BD983-4695-4D7C-A9EE-854749B1D70B@.microsoft.com...
> Thanks all for your replies. If I understand the explanations correctly,
> what I am interpreting is that
> CREATE TRIGGER my_trig
> ON my_table
> FOR INSERT
> ...
> is the same as
> CREATE TRIGGER my_trig
> ON my_table
> After INSERT
> ...
> Is this correct? This is where my confusion lies.
> Thanks,
> Rich|||Thanks. But to take this one step further, since For and After seem to be
the same, is it possible to do this?
CREATE TRIGGER my_trig
ON my_table INSERT
...
where I don't include a For or After? Is this what is meant by default
trigger?
"Jerry Spivey" wrote:

> Yes.
> "Rich" <Rich@.discussions.microsoft.com> wrote in message
> news:E21BD983-4695-4D7C-A9EE-854749B1D70B@.microsoft.com...
>
>|||No.
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:11EBA02F-433D-410A-ABAD-34CAB173E453@.microsoft.com...
> Thanks. But to take this one step further, since For and After seem to be
> the same, is it possible to do this?
> CREATE TRIGGER my_trig
> ON my_table INSERT
> ...
> where I don't include a For or After? Is this what is meant by default
> trigger?
>
> "Jerry Spivey" wrote:
>|||Yes; in earlier versions of SQL Server we only had one kind of trigger, so
we didn't need to say instead of or after. We had a trigger FOR an
operation, and it always fired AFTER the operation took place.
When INSTEAD OF triggers were added, we needed a way to make sure we
differentiated the two kinds of triggers, so AFTER was introduced as a
synonym for FOR.
So really, there is no default. You have to specify when the trigger fires,
either with FOR or AFTER (which are the same) or INSTEAD OF.
--
HTH
Kalen Delaney, SQL Server MVP
www.solidqualitylearning.com
"Rich" <Rich@.discussions.microsoft.com> wrote in message
news:E21BD983-4695-4D7C-A9EE-854749B1D70B@.microsoft.com...
> Thanks all for your replies. If I understand the explanations correctly,
> what I am interpreting is that
> CREATE TRIGGER my_trig
> ON my_table
> FOR INSERT
> ...
> is the same as
> CREATE TRIGGER my_trig
> ON my_table
> After INSERT
> ...
> Is this correct? This is where my confusion lies.
> Thanks,
> Rich
>|||Thanks everyone, again for all the explanations. So if I am not using an
Instead Of trigger, then I can write
Create Trigger...For Update...For Insert...For Delete
or
Create Trigger...After Update...After Insert...After Delete
And it is the same thing. I think I get it now.
Many thanks for all the help.
Rich
"Rich" wrote:

> Hello,
> I am a little between For Insert and After Insert statements on
> Triggers. I just need to know which is the Sql Server Default Trigger. H
ere
> are 2 sample Triggers. Is For Insert or After Insert the default?
> ---
> CREATE TRIGGER my_trig
> ON my_table
> FOR INSERT
> AS
> IF UPDATE(b)
> PRINT 'Column b Modified'
> GO
> ---
> Create Trigger Update_Status
> On ACRPLU AFTER INSERT
> As
> Update A Set A.a1updt = 'D'
> From ACRPLU A
> Where (A.a1updt = '' Or IsNull(A.a1updt, -1) < 0) And A.PK
> In(Select Distinct PK From Inserted)
> ---
> Thanks.
> Rich
>

No comments:

Post a Comment