如果新值相同,SQL是否会更新记录?

时间:2022-03-31 15:43:58

Will SQL update a record if there is no change to the record?

如果记录没有变化,SQL会更新记录吗?

For examople, is it more efficient to replace

例如,更换效率更高

UPDATE TABLE myTable 
Set Col1 = ISNULL(Col1,'')
...
Set Col100 = ISNULL(Col30,'')

with

UPDATE TABLE myTable 
Set Col1 = ISNULL(Col1,'')
...
Set Col100 = ISNULL(Col30,'')
WHERE 
Col1 IS NULL OR
...
Col30 IS NULL

2 个解决方案

#1


5  

Yes, it will attempt overwrite.

是的,它会尝试覆盖。

#2


1  

You have to control it manually, in the where clause you can put all the fields if they are differ the new values, and where the id of your table is equals to your parameter, you will ensure that only modified records will be updated.

您必须手动控制它,在where子句中,如果新值不同,可以放置所有字段,并且表的id等于您的参数,您将确保只更新已修改的记录。

    UPDATE table
    SET field1 = @field1,
                 field2 = @field2
    WHERE field1 != @field1 AND
          field2 != @field2 AND
          id = @id

#1


5  

Yes, it will attempt overwrite.

是的,它会尝试覆盖。

#2


1  

You have to control it manually, in the where clause you can put all the fields if they are differ the new values, and where the id of your table is equals to your parameter, you will ensure that only modified records will be updated.

您必须手动控制它,在where子句中,如果新值不同,可以放置所有字段,并且表的id等于您的参数,您将确保只更新已修改的记录。

    UPDATE table
    SET field1 = @field1,
                 field2 = @field2
    WHERE field1 != @field1 AND
          field2 != @field2 AND
          id = @id