I have a table with two columns where I need one (columnB) to be a copy of the other one (columnA). So, if a row is inserted or updated, I want the value from columnA to be copied to columnB.
Here’s what I have now:
CREATE TRIGGER tUpdateColB
ON products
FOR INSERT, UPDATE AS
BEGIN
UPDATE table
SET columnB = columnA
END
The problem now is that the query affects all rows, not just the one that was updated or inserted. How would I go about fixing that?
Assuming you have a primary key column,
id, (and you should have a primary key), join to theinsertedtable (making the trigger capable of handling multiple rows):But if ColumnB is always a copy of ColumnA, why not create a Computed column instead?
Using the inserted and deleted Tables