Monday, December 8, 2008

SQL Server - Cannot attach a database that was being restored

This was a fun error. We had an extremely large database (450 GB) that was in the process of restoring and got hung up in SQL Server 2005. Each time we would try to attach the database, it would fail because it kept thinking it was "being restored". So, after you take some aspirin, be assurred that there is a way to "trick" SQL Server.

  1. Create a new database with the same name as the one you are trying to attach.
  2. (optional) Detach the database, and then move the MDF and LDF files to the specific drives that you need.
  3. (optional) Reattach the database from it's new location. You will have to update the paths to the LDF file when restoring.
  4. Take the database Offline.
  5. Copy your original MDF and LDF files and overwrite the newly created files.
  6. Bring the database back Online.

Wah-la! It's back!

If this didn't work for you, you may have something more severe on your hands like a corrupt database file.