By mistake update few data and how to rollback those changes later

Sudip Bhatt 2,281 Reputation points
2020-09-16T09:39:19.47+00:00

i am curious to know if by mistake i update few data in table then how could i rollback those changes.

same is applicable for insert and delete too. please share some sql which help me to rollback the data for insert/update and delete. thanks

Developer technologies | Transact-SQL
{count} vote

5 answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2020-09-16T19:57:15.517+00:00

    SQL Server does not store that data after the transaction is "committed". So there generally no way in SQL Server to recover the previous version of the data.

    The data may exist in the log file for a certain amount of time. However, this is not guaranteed and not a great way to recover data from prior transactions.

    If you need a "history" of changes, you will need to implement temporal tables or CDC or some other method to capture the data BEFORE you have a problem.

    2 people found this answer helpful.

  2. Olaf Helper 47,441 Reputation points
    2020-09-16T10:05:41.227+00:00

    If the database is in full recovery mode, then you can take a log backup and perform a point-in-time restore, but that will also undo wanted data changes.

    1 person found this answer helpful.
    0 comments No comments

  3. Viorel 122.6K Reputation points
    2020-09-16T10:28:39.333+00:00
    1 person found this answer helpful.

  4. Dan Guzman 9,406 Reputation points
    2020-09-16T10:31:31.32+00:00

    Data modification statements are automatically committed by default. In order to undo changes, you'll need to start an explict transaction using BEGIN TRANSACTION, execute INSERT/UPDATE statements, and finally execute a COMMIT (to make changes permanent) or ROLLBACK (to undo uncommitted changes) as desired. One cannot undo data modifications once committed without restoring the database.

    Adding to @Olaf Helper 's answer, you can recover data without overwriting other changes by restoring the database to a different name from a full backup, restoring log backups for point-in-time recovery (if possible), and then writing queries to salvage data from the restored copy.

    1 person found this answer helpful.
    0 comments No comments

  5. MelissaMa-MSFT 24,221 Reputation points
    2020-09-17T03:20:10.697+00:00

    Hi @Sudip Bhatt ,

    Please refer some suggestions from below:

    1. Create a trigger or use a tool like ApexSQL to record the transaction log of insert,update and delete actions. Then you could restore the few data manually.
    2. Make sure the database where this table locates is in full recovery mode and take full,differential and log backups frequently. Then you could perform a point in time restore.
    3. Based on 2, you could recover a single table from a SQL Server database backup.
    4. Create another table as backup of this table and restore the data accordingly.
    5. Add a transaction and try statement before and after the update statement.
          BEGIN TRY  
      BEGIN TRANSACTION  
              Select/update/delete                      
             COMMIT TRANSACTION   
      END TRY  
      BEGIN CATCH  
              ROLLBACK TRANSACTION  
              -- Consider logging the error and then re-raise  
              --select ERROR_MESSAGE()  
      END CATCH  
      

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    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

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.