Search code examples
sql-serverrestore

Restore deleted information from active SQL Server table


I have a database that was storing customer information (dbo.custinfo). The telephone numbers in the table were removed in the past for security purposes by replacing with Null values. Now it has been decided that the phone numbers need to be put back in.

There have been other customers added to this database since then. What is the best way to restore this data while making sure the correct numbers correspond to the correct customers?

I have the backup of the db that was done before removing the phone numbers.

Thank you in advance


Solution

  • While in accordance with Larnu comment the side by side restore of the database is required, this operation can be accomplished by SSMS and GUI.

    However, there is no wizard for building update statement that uses joins, so perhaps it is kind of advanced case for someone who are not specialized in SQL.

    So, a generic snippet to get values back:

    -- Another backup to rollback this change
    BACKUP DATABASE yourDB TO DISK= 'C:\Somewhere\YourDB.bak' WITH COPY_ONLY, COMPRESSION, STATS=1
    -- UPDATE + JOIN
    UPDATE cur_ci
    SET cur_ci.phone = res_ci.phone
    FROM [CURRENTDB].dbo.custinfo cur_ci
    JOIN [RESTOREDB].dbo.custinfo res_ci on cur_ci.customerID = res_ci.customerID