Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
INSTEAD OF Triggers
Message
General information
Forum:
Microsoft SQL Server
Category:
Other
Environment versions
SQL Server:
SQL Server 2008
Miscellaneous
Thread ID:
01467449
Message ID:
01467460
Views:
29
That is a good point, one that had not hit me yet. The UPDATE in the trigger I worked on the other night is limited to one record, so that is okay in that case, but I hear you, in that multiple records coudl be updated.
UPDATE Invoices2
SET PaymentTotal = 503.20
WHERE InvoiceID = 105
>If a single UPDATE statement affects multiple invoice rows, you will only catch the first vendor. (This is a common "mistake" - many people write trigger code with the assumption that the UPDATE statement will only ever affect one row).

>Unless you're 100% certain that an UPDATE will ONLY affect 1 row, always write update triggers with the assumption that multiple rows are being affected. (If you weren't already aware of this...if I type "UPDATE Invoices set Discount = Discount + .1 " , to affect all rows, the trigger fires ONCE and the DELETED/INSERTED tables will contain before/after values for all rows affected by the UPDATE statement).

>So if you're sure that only 1 invoice will EVER get updated at once, then your code above is OK....but if someone were to do a mass update where multiple invoices and multiple vendors were affected, your code would only capture one of the vendors. (And if you had typed out...SET @Vendorname = (select blah blah blah) to capture the vendor that way, it would actually generate an error).
Previous
Reply
Map
View

Click here to load this message in the networking platform