tags:

views:

70

answers:

2

Hello,

I have a trigger AFTER INSERT inside which I must update the value of a column of the row that has been just inserted. I obviously cannot use :

SET new.column = value;

I've tried to do a manual update in the trigger but it's also not allowed.

Is there any simple way to work this out ?

Many thanks !

+2  A: 

An AFTER trigger is typically used to update something other than the row being updated. For example, if you wanted to log the fact that an update had been made, an AFTER trigger is ideal.

To change the value of a column as it is being inserted, you need to use a before trigger. For example

CREATE TRIGGER modify_column BEFORE INSERT ON mytable SET @column = value;

Where value is a query, pre defined value or NEW.column

Robert Christie
Thanks ! But can't I do both in the same trigger (the AFTER one, that is)
Amadeus45
@Amadeus45: That is the difference between a BEFORE trigger and an AFTER trigger. You can only change the new row BEFORE it is written to the database.
Bill Karwin
A: 

Since the INSERT is already done by the time the AFTER is fired, I think you will have to write T-SQL to change the value based on your primary key.

If you want to change it before it gets inserted, you may want to consider moving to a BEFORE trigger instead.

Raj More