Problem

When attempting to attach a company database to SQL Server using SQL Server Management Studio you receive error 9003 with a message similar to this:

The log scan number %S_LSN passed to log scan in database '%.*ls' is not valid. This error may indicate data corruption or that the log file (.ldf) does not match the data file (.mdf). If this error occurred during replication, re-create the publication. Otherwise, restore from backup if the problem results in a failure during startup.

Cause

This can be caused when the database log file (.ldf) corrupt. SQL Server cannot attach a database file (.mdf) with a matching corrupt log file.


Solution

The best solution is to restore from a recent backup. However, if a recent backup is not available then you can delete the corrupted log file and then attach the database. SQL Server will then re-create the log file.


Note: It's possible that data loss will occur if you delete the log file so only perform this process if you don't have a recent backup.


To delete the log file

  1. In Windows Explorer navigate to the folder that contains the database files (.mdf and .ldf) that you are attempting to attach.
  2. Delete the .ldf file.
  3. Run SQL Server Management Studio.
  4. Right-click on Databases and select Attach.
  5. Click the Add button, select the .mdf file and click OK.
  6. On the Attach dialog, in the Database details section, select the log file (.ldf) and click Remove.
  7. Click OK to attach the database. A new log file will be created.