Recovering a SQL Database with only datafile available (no log or backup)

Assuming no database set up in SQL Server this should attach the database and rebuild the log providing the MDF datafile was in a consistent state when it was copied.
CREATE DATABASE <database>
ON (FILENAME = ‘D:\<fullpath>\<database>.mdf’)
FOR ATTACH_REBUILD_LOG
If the below message or similar occurs:
File activation failure. The physical file name “<Datafile>” may be incorrect.
The log cannot be rebuilt because the database was not cleanly shut down.

Then method 2 will trick the database into allowing a database to attach in recovery pending mode.  However this could mean some datal oss occurring as the database has to be repaired, and this is not guaranteed to work.

  1. Create a new database (empty)
  2. Switch off the instance
  3. Rename the original MDF file to the new database new file.mdf overwriting the existing file.
  4. Delete the .ldf file for the new database
  5. Start the Instance (db will start in recovery pending mode)
  6. ALTER DATABASE <database> SET EMERGENCY
  7. ALTER DATABASE <database> SET SINGLE_USER
  8. DBCC CHECKDB(<database>,REPAIR_ALLOW_DATA_LOSS)
  9. ALTER DATABASE <database> SET MULTI_USER
Repair took 15 minutes on a 210Gb database.
External Reference here: http://www.sqlinthewild.co.za/index.php/2009/06/09/deleting-the-transaction-log/

Comments

Popular posts from this blog

SQL SERVER – Event ID 107- Report Server Windows Service (MSSQLSERVER) cannot connect to the report server database.

SQL Server Builds Information

Using DBCA silent install and disabling automatic memory management