Search code examples
sqlrestore

Copy column of table from backup database to main database with WHERE clauses


I accidentally deleted some rows from the database. But I already have daily database backups.

How can I restore only deleted records from the backup database?

Thanks in advance


Solution

  • You didn't say a database server, so I can't be sure this will work, but I believe the syntax on MSSQL would be:

    UPDATE livefile
    SET livefile.bloodtypefield=oldfile.bloodtypefieild
    FROM [hospital].[dbo].[tblPatientFile] livefile
    INNER JOIN [hospitalRapor].[dbo].[tblPatientFile] oldfile on livefile.patientid=oldfile.patientid
    

    I highly recommend running on a test database first to make sure it has the results you want. You will of course need a user who has access to both databases and depending on whether you have triggers etc defined that may take a long time to run on 400k rows.