I want to create a trigger in SQL Server 2012 that activates when I insert a row into a specific table.
The trigger must insert a value in a specific column, in the same table and row I am inserting the new row into.
The trigger must only activate if a specific column in the row I am inserting does not have a value ie. null.
Let's say I am inserting into:
- Table:
car - Columns:
car_make | car_price | car_img
If I provide all values in the insert:
car_make | car_price | car_img
the trigger must not activate
But, if I only insert values for 2 columns:
car_make | car_price
the car_img value is null, then trigger must activate and do the following:
- Insert a predefined value in the column
car_imgin the same affected row
PS. I have to use a trigger, it's for an assignment. I cannot use a default value.
FOR INSERTtrigger, it will fire always - you cannot selectively let it fire (or not) depending on your data. What you can do is check for these conditions in the trigger code and handle the situation as needed