Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
UPDATE() in triggers
Message
From
23/10/2003 08:18:24
 
 
To
23/10/2003 01:52:41
Jenny Karlsson
Megasol Technologies
Uppsala, Sweden
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
00841063
Message ID:
00841494
Views:
27
A SQL Server trigger is fired once regardless of the number of rows that are affected, even if zero rows were affected by the query. Given a query such as:

UPDATE mytable SET column1=? WHERE ...

Every row that is affected by query has had its column1 column modified. This is why you only have to check using IF UPDATED(column1). Now, say that you're doing something like this:

UPDATE mytable SET
column1 = CASE WHEN ... THEN column1=column1*2 ELSE column1 END
WHERE
...

From SQL Server's POV, the column column1 is being modified regardless of the outcome of the conditional in the CASE expression. So IF UPDATE(column1) will always indicate this. If you only want your trigger to affect rows that actually had the value change, you'll have to compare the rows in the Inserted and the Deleted tables. The Deleted table will contain a copy of the rows before the modifications occurred, and the Inserted will contain a copy of the rows after the modifications occurred. TSQL supports an extension to the UPDATE statement that allows you to include a FROM clause and reference other table. Check the BOL.

-Mike


>Hi!
>
>I think my question was a little bit fuzzy. I understand that the inserted table contain all the rows that have been updated or inserted (for an update/insert trigger), but out of all these rows in inserted table, I only want the rows where a particular field have been updated, for example if idkey have been updated it would be in inserted BUT I only want this row if the field amount have been updated. The use of a cursor in this case if because of for each of these rows where amount have been updated I would like to call a procedure.
>
>/Jenny
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Previous
Reply
Map
View

Click here to load this message in the networking platform