It's good pratics to use exists before an update?

first100 81 Reputation points
2022-02-16T16:35:49.787+00:00

Hello,

as a title, I ask if it is a good idea to check if a record with a certain value exists first, before updating that record, because if the check is not done the update does nothing so it does not give side effects, so what is the best practices?

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Tom Phillips 17,771 Reputation points
    2022-02-16T17:40:56.49+00:00

    No. Checking to see if a record exists, and then updating the existing record causes 2 scans of the table. There is never a reason to do that. If you need to know if a record was actually updated you can use @@ROWCOUNT.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. LiHong-MSFT 10,056 Reputation points
    2022-02-17T03:16:21.5+00:00

    Hi @first100
    As Tom said,there no need to check if a record exists before update.
    If you have lots of records and don't want to manually check one record at a time to see if data in a certain column already exist.
    You could use a left join (or EXISTS etc...) from the source to the destination table on the id column to check if you have a matching row or not.
    You can also have a look at the MERGE statement: https://msdn.microsoft.com/en-us/library/bb510625.aspx
    Also,here is a link which is related to this thread : If Exists before Update or Delete?

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Yitzhak Khabinsky 26,586 Reputation points
    2022-02-17T14:07:19.787+00:00

    Hi @first100 ,

    Here is one way to handle it.
    It will update rows just and only if there are some differences in place.

    SQL

    -- DDL and sample data population, start  
    DECLARE @Source TABLE (APK INT IDENTITY PRIMARY KEY, ID_NUMBER INT, UpdatedOn DATETIMEOFFSET(3));  
    DECLARE @Target TABLE (APK INT IDENTITY PRIMARY KEY, ID_NUMBER INT, UpdatedOn DATETIMEOFFSET(3));  
      
    INSERT INTO @Source (ID_NUMBER)  
    VALUES (null), (null), (7), (7), (5);  
      
    INSERT INTO @Target (ID_NUMBER)  
    VALUES (null), (7), (null), (7), (4);  
    -- DDL and sample data population, end  
      
    SELECT * FROM @Source;  
    SELECT * FROM @Target;  
      
    UPDATE T   
    SET T.ID_NUMBER = S.ID_NUMBER  
       , T.UpdatedOn = SYSDATETIMEOFFSET()  
    FROM @Target AS T  
        INNER JOIN @Source AS S  
          ON T.APK = S.APK  
    WHERE NOT EXISTS (SELECT S.* INTERSECT SELECT T.*);  
      
    -- test  
    SELECT * FROM @Target;  
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.