Saturday, December 14, 2013

SQL 2008 - How to recover a damaged or corrupted mdf or ldf file


For this example, we will assume the database name is "TEST" with the files test.mdf and test.ldf.
When we try to attach the mdf, we get an error message saying the file is corrupted.

  1. Rename the TEST.mdf, and TEST.ldf  to TEST_OLD.mdf and TEST_OLD.ldf
  2. Create TEST as a database so that it generates a blank mdf and ldf
  3. Stop SQL server service
  4. Delete the blank test.mdf and test.ldf, and rename the test_old.mdf and test_old.ldf to test.mdf and test.ldf to force sql to read the corrupted files
  5. Start SQL server service
  6. The database will show that it is in "suspect" mode
  7. Run this script taken from
    http://www.codeproject.com/Articles/20333/How-to-Restore-SQL-Server-2005-Suspect-Database

  8. EXEC sp_resetstatus 'yourDBname';
    ALTER DATABASE yourDBname SET EMERGENCY
    DBCC checkdb('yourDBname')
    ALTER DATABASE yourDBname SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    DBCC CheckDB ('yourDBname', REPAIR_ALLOW_DATA_LOSS)
    ALTER DATABASE yourDBname SET MULTI_USER
  9. This will most likely cause a fair amount of data loss, but the database will be accessible

1 comment:

  1. For non technical user, it is not an easy task to repair sql database mannually, i would recommend to such people, go for third party application, which is easy and fast way to recover sql database. Go to Google search there lot of tools available there, one such application is here:
    http://www.recoverydeletedfiles.com/sql-database-recovery-software.html

    ReplyDelete