PRB: IF UPDATE Trigger with No Rows Changed (64238)
The information in this article applies to:
- Microsoft SQL Server 4.2x
- Microsoft SQL Server 6.0
- Microsoft SQL Server 6.5
- Microsoft SQL Server 7.0
- Microsoft SQL Server 2000 (all editions)
This article was previously published under Q64238 SYMPTOMS
A trigger has been defined using the IF UPDATE(ColName) clause.
When attempting an update on the column, even if the update
statement does not actually affect any rows, the statements in the
IF UPDATE block get executed. IF UPDATE should be true only for
columns actually updated by a query; however, it returns true when
no rows were updated.
CAUSE
The T-SQL reference states that IF UPDATE is used to test whether the specified column has been modified. IF UPDATE actually returns true whenever the specified column is included in an update statement. When an INSERT or UPDATE statement is executed, the affected rows are added to a special table called "INSERTED."
WORKAROUND
By adding the line
and (select count(*) from inserted) > 0
just after the IF UPDATE clause, your trigger will be invoked only
when at least one row is actually modified. Below is a SQL script
that shows how this could be implemented.
Modification Type: | Minor | Last Reviewed: | 2/14/2005 |
---|
Keywords: | kbother KB64238 |
---|
|