sql-serversql-server-2005mdfdata-recovery

How to recover database from MDF in SQL Server 2005?


I have an MDF file and no LDF files for a database created in MS SQL Server 2005. When I try to attach the MDF file to a different SQL Server, I get the following error message.

The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.

I would like to accomplish any one of the following options:

  1. Attach the database without data loss (unlikely but would save me some time).
  2. Attach the database with data loss (whatever transactions were open are lost).
  3. Recover the schema only (no data) from the MDF file.

What SQL commands can I try to get my database going again?


Solution

  • I found the following document on Experts Exchange.

    patrikt: You will have data loss but it can be done.

    1. Detach database and move your mdf to save location.
    2. Create new databse of same name, same files, same file location and same file size.
    3. Stop SQL server.
    4. Swap mdf file of just created DB to your save one.
    5. Start SQL. DB will go suspect.
    6. ALTER DATABASE yourdb SET EMERGENCY
    7. ALTER DATABASE yourdb SET SINGLE_USER
    8. DBCC CHECKDB (yourdb, REPAIR_ALLOW_DATA_LOSS)
    9. ALTER DATABASE yourdb SET MULTI_USER
    10. ALTER DATABASE yourdb SET ONLINE