How To Attach A SQL Server Database MDF File Without A Log File



TODO:

Recently I had a corrupt log file, and my database was continuously 'Suspect'.  Since this was a test database, I did not care about my corrupt .ldf file.  Therefore, I simply wanted to attach my .mdf file and let it create a new .ldf file.  The SQL below will do just that.

 

SOLUTION:

CREATE DATABASE myDatabase ON
( FILENAME = N'G:\DATA\myDatabase.mdf')
FOR ATTACH
GO

 

NOTES:

This method works when there is only one log file and it is missing.

IF there is more than 1 log file, use 'FOR ATTACH_REBUILD_LOG' rather than 'FOR ATTACH'

 



Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading