Question - is SQL Server (2008) smart enough to not physically update
field, if it was assigned a value, as in the case if @ field1dirty = 0?
No, you should add a where clause that says ... where field <> the value you are updating to .
It doesn't seem like a big deal at first, but it can actually create a huge amount of overhead. One example, think of triggers. If this is an update of each field in the table, this trigger will fire for each row. YIKES, that a lot of code execution, which is unnecessary, especially if this code says, moves update lines to the log table. I'm sure you understand this idea.
Remember that you are updating the field, it is just the same value as it was before. It is really good that this happens because it means that you can still consider the field changed (think about a timestamp, etc.). If he did not think that updating the field to the same value changed the line, you would not know if someone was unintentionally (or intentionally) trying to change the data.
Update due to comments: link to coalesce function
Example: To handle null parameter values in a stored procedure
Update Table SET My_Field = COALESCE(@Variable, My_Field)
This does not mean what I said before when the field is updated to the same value, but it allows you to check the parameter and conditionally update the field.
source share