views:

1308

answers:

4

How can I attach a database without an LDF file in SQL Server?

+5  A: 

Hi Ricardo,

You can use sp_attach_single_file_db to attach a database which is missing it's log file.

Aaron Alton
It gives this error:Could not open new database 'SharedCS'. CREATE DATABASE is aborted.Device activation error. The physical file name 'd:\Program Files\Microsoft SQL Server\MSSQL\data\dummy_Log.LDF' may be incorrect.
Ricardo
Don't try to attach a dummy log. Can you post the sp_attach_single_file_db command you're running?
Aaron Alton
+1  A: 

You can "just do it" it'll throw a warning that it couldn't find the .ldf, but it will still attach the db.

Kyle West
It gives an error and the database is not attached.
Ricardo
+1  A: 

You can try what is posted here by MohammedU. Basically, what he uses the DBCC REBUILD_LOG command. It will work depending on the version of your server.

Here are the steps (without details):

  1. Rename existing .mdf file to .mdf_old
  2. Create a new database with same .mdf and .ldf file as old one.
  3. Stop the sql server
  4. Rename .mdf and .ldf files of the new db to .mdf_old and .ldf_old
  5. Rename .mdf_old to .mdf
  6. Start sql server
  7. You should see db in suspect mode
  8. Change the database context to Master and allow updates to system tables
  9. Set the database in Emergency (bypass recovery) mode.
  10. Stop and restart SQL server.
  11. Rebuild the log.
  12. Set the database in single-user mode and run DBCC CHECKDB to validate physical consistency.
  13. Turn off the updates to system tables.
eKek0
A: 

If you run into problems, verify that the mdf file is not read-only.