I don't know if this is possible, but I have a column named active
in a table. Whenever the active column gets changed, I would like to reset the date in the date
column, but ONLY if the active
column gets changed.
If other columns are changed but not the active
column, then the date would remain the same.
In SQL Server, you can create DML triggers that execute code only when a specific column is updated. The trigger still fires, but you can test whether or not a specific column was updated, and then run code only if that column was updated. You can do this by using the UPDATE() function inside your trigger.
SQL Server COLUMNS_UPDATED() Function for Triggers. This function is used to know the inserted or updated columns of a table or view. It returns a VARBINARY stream that by using a bitmask allows you to test for multiple columns.
An UPDATE trigger can refer to both OLD and NEW transition variables. INSERT.
A new column value can be assigned in a BEFORE row trigger, but not in an AFTER row trigger (because the triggering statement takes effect before an AFTER row trigger is fired). If a BEFORE row trigger changes the value of new .
something like
DELIMITER // CREATE TRIGGER updtrigger BEFORE UPDATE ON mytable FOR EACH ROW BEGIN IF NEW.active <> OLD.active THEN SET NEW.date = ''; END IF; END //
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With