Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 with the values in the update statement ?

For example:

I have TableA with column FirstName whose value is 'Roger Moore' with an unique id of '007'. Now, i am calling an update statement but it should update the 'FirstName' field only if value is something else other than 'Roger Moore'.

TIA

like image 659
SoftwareGeek Avatar asked Feb 16 '26 14:02

SoftwareGeek


1 Answers

update TableA 
set FirstName = 'Roger Moore' 
where ID = '007' 
    and FirstName <> 'Roger Moore' 
like image 142
D'Arcy Rittich Avatar answered Feb 19 '26 12:02

D'Arcy Rittich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!