SQL Server - update only when values ​​are different

In SQL Server 2008, how do I update fields in a table only if their values ​​differ from the values ​​in the update statement?

For instance:

I have a TableA with a FirstName column whose value is "Roger Moore" with a unique ID of "007". Now I call the update statement, but it should only update the "FirstName" field if the value is anything other than "Roger Moore".

TIA

+2


a source to share


3 answers


update TableA 
set FirstName = 'Roger Moore' 
where ID = '007' 
    and FirstName <> 'Roger Moore' 

      



+7


a source


As a rule, it is not worth trying to check the field change

http://blogs.msdn.com/queryoptteam/archive/2006/07/07/659453.aspx



The benefits of blocking are likely to be fulfilled on a conditional update.

However, if you insist on it, you can also look at it "in the background" instead of "trigger" and compare each inserted vs the current values.

+1


a source


Are you talking about an UPDATE statement like this?

UPDATE TableA
   SET FirstName = 'New Name'
 WHERE FirstName <> 'Roger Moore'

      

This does not account for your unique ID, but I think this is what you are looking for.

0


a source







All Articles