However, this is the main red flag if you expect to set variable values similar to this in a trigger. This usually means that the trigger is poorly designed and needs to be reviewed. This code expects only one record to be inserted, and in all cases this will not be true. Insertion or updating of several records will contain several records, and the trigger should take this into account (without using the trigger !!!). Triggers should never be written to handle only single-line inserts / updates or deletes. They must be recorded to process data sets.
Example for inserting values from inserted into another table, where the trigger is in table1:
CREATE TRIGGER mytrigger on table1 AFTER INSERT AS INSERT table2 (field1, field2, field3) SELECT field1, 'test', CASE WHEN field3 >10 THEN field3 ELSE 0 END FROM inserted
Hlgem source share