100% found this document useful (3 votes)
256 views

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.

Uploaded by

Dazy Parker
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
256 views

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.

Uploaded by

Dazy Parker
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

By SQL Server Management Studio:


To fix this issue by the SQL Server Management Studio, just follow the simple steps
1. First connect the instance of SQL Server using SSMS.
2. Go to the Object Explorer then, Right Click on the Database node. You will
see a drop-down menu.
3. Click on the Attach Tab.
4. Click on the Add Button.
5. Now you see the locate Database Files dialog box.
6. Now click on the brows button to select the .mdf file of your database then
click on the OK button.
7. Now you can see the selected .mdf file but the SQL Server is unable to select
the .ldf file because it is missing.
8. To attach the .mdf file without log file; select the log file and remove it.
9. Now click on the OK button to attach the database.
10.Now SQl server will create a log file while attaching the database.
These are the few simple steps to attach the .mdf file without .ldf file. One
suggestion from my side is that, user should run the DBCC CHECKDB command on
the database because it will check and report the error messages.

You might also like