简体   繁体   中英

Restore DB from MDF file, lost LDF and DB was not cleanly shutdown

My dev DB server was formatted by some admin without my knowledge. Thankfully I had .mdf file kept in some other drive but .ldf was in its default location. Since I don't have DB backup I am now left with .mdf file. I googled and found various tricks to restore DB from .mdf file. Since database was not cleanly shutdown none of the tricks worked. I have SQL Server 2008 SP2 installed in my machine. As I remember lost DB server was SQL Server 2005 instance. Please let me know how I could recover my database.

Tricks i tried:

  1. I tried to attach the .mdf from SSMS by removing the log file item from database to attach list.
  2. CREATE DATABASE Monitoring ON (FILENAME = N'D:\\Dump\\Data\\Monitoring.MDF') FOR ATTACH_REBUILD_LOG
  3. sp_attach_single_file_db N'Monitoring', N'D:\\Dump\\Data\\Monitoring_Data.MDF'

In all above I got

Incorrect DBCC statement. Check the documentation for the correct DBCC syntax and options.

Then I tried DBCC REBUILD_LOG('Monitoring',N'C:\\Program Files ..MSSQL\\Data\\Monitoring_log.ldf') which resulted in:

Incorrect DBCC statement. Check the documentation for the correct DBCC syntax and options.” error.

Here are some notes (for SQL 2000) that might help. I've never tried these personally.

  1. In EM create a DB with the same name as the original.
  2. Stop the SQL service (instance).
  3. Copy the old .MDF over the new one.
  4. Start the service (instance).
  5. DB will appear as suspect in EM.
  6. Fire up Query Manager.
  7. Connect to the running instance.
  8. Put the DB into Emergency Bypass Status by doing the following:

     sp_configure 'allow', 1 go reconfigure with overide. update master..sysdatabases set status=32768 where dbid=5 --check this ID is for your db 
  9. Stop the SQL service (instance).

  10. Delete the LDF.
  11. Start the SQL service (instance).
  12. Execute the following in QM:

     select dbid, status, name from master..sysdatabases dbcc rebuild_log ('dbname', 'full path and LDF name') 
  13. Revert DB from Emergency Bypass Status by executng in QM:

      update master..sysdatabases set status=16 where dbid=5 --Check this ID for your db 
  14. Cleanup by executing the following in QM:

      sp_configure 'allow', 0 go reconfigure with override 

Try This:

ALTER DATABASE EmpDataBase REBUILD LOG ON (NAME=$DatabaseName$,FILENAME=N'$Path\\DatabaseName.ldf$')

GO

DBCC CHECKDB

ALTER DATABASE $DatabaseName$ SET MULTI_USER

GO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM