Sunday, April 25, 2010

Attach MDF file in case of missing LDF file

In case you have a database with .mdf file and without having the .ldf file of a database so to restore your database or reattach it again do the following:
First open SSMS and right click in Database folder in object explorer and choose Attach….


Then click on Add button to add .mdf file of a database you need to attach it as the following


Note the Message column of .ldf file shows (Not Found) so select this row and then remove it as the following

Then press OK to reattach your database with new .ldf file.
Or use the following command to attach database file with only .mdf file:

USE [master]
GO
CREATE DATABASE [TestDb] ON 
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\TestDb.mdf' )
 FOR ATTACH
GO


and you will get the following result:
File activation failure. The physical file name "C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\TestDb_log.ldf" may be incorrect.

New log file 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\TestDb_log.LDF' was created.

No comments: