Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will SQL update a record if the new values are the same?

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

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
like image 214
Chad Avatar asked Jun 04 '12 13:06

Chad


People also ask

What will happen if two persons are updating the same record in a table at the same time?

there will be occasions where two users will both read the same data into memory. User 1 will update the data and write those changes back to the database before user 2 does the same thing. Now you have a concurrency control conflict because user 1 read the data before user 2 wrote it back to the database.

Does update in SQL overwrite?

If you tell SQL Server to UPDATE some rows - it will update those rows. SQL Server doesn't do any "matching" of its own. That's up to you - you control the WHERE clause for the UPDATE . If you want to avoid updating some rows - make sure the WHERE clause excludes them.

Can you update the same column in SQL?

The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement.

How can I tell if a SQL record is updated?

To get the last updated record in SQL Server: We can write trigger (which automatically fires) i.e. whenever there is a change (update) that occurs on a row, the “lastupdatedby” column value should get updated by the current timestamp.


2 Answers

Yes, it will attempt overwrite.

like image 117
Romil Kumar Jain Avatar answered Sep 28 '22 08:09

Romil Kumar Jain


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.

    UPDATE table
    SET field1 = @field1,
                 field2 = @field2
    WHERE field1 != @field1 AND
          field2 != @field2 AND
          id = @id
like image 22
crassr3cords Avatar answered Sep 28 '22 08:09

crassr3cords