Attach .MDF File Without .LDF File in SQL Server
Attach .MDF File Without .LDF File in SQL Server
Every database is useful for the user and we have to take the proper backup of our
database. This blog is about attaching an .MDF file without .LDF file and I will
describe the different methods to do this process. Both files are very important for
the database; .MDF file is the Master Database and .LDF is transactional log file
which keeps the details of all transactions of the database.
I will show you two different ways to attach an .MDF file without .LDF file:
1. By SQL commands
2. By SQL Server Management Studio
By SQL Commands
There are multiple ways to do this and normally I use methods 1.
USE [master]
GO
Method 1:
EXEC sp_attach_single_file_db @dbname='TestDatabase',
@physname=N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSS
QL\DATA\TestDatabase.mdf'
GO
Method 2:
CREATE DATABASE TestDatabase ON
(FILENAME =N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQ
L\DATA\TestDatabase.mdf')
FOR ATTACH_REBUILD_LOG
GO
Note: Above command will try to attach the database file create an empty log file
while attaching the database. Many experts recommend this command if you have
more than one missing log files.
Method 3:
CREATE DATABASE TestDatabase ON
( FILENAME =N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSS
QL\DATA\TestDatabase.mdf')
FOR ATTACH
GO
It will work if there is one log file that is missing. If there are more than one log files
then, all of them are required to go through the same procedure.