Recovery

How to Attach an MDF file without the LDF

How to Attach an MDF file without the LDF
Quick Answer

To attach an MDF without the LDF, let SQL Server rebuild the log. Run CREATE DATABASE with FOR ATTACH_REBUILD_LOG, pointing at the MDF, and SQL Server creates a fresh log and brings the database online. This works cleanly and loses nothing when the database was shut down properly. If it was not, the rebuild can discard uncommitted transactions, and if the MDF itself is corrupt no attach method works, which is where the Univik SQL Database Recovery tool reads the MDF directly instead.

Losing the LDF and keeping the MDF is a common way to end up locked out of a database. The log file gets deleted to free space, goes missing after a move, or corrupts in a crash, and suddenly SQL Server will not attach the data file without it. The good news is that a transaction log can be rebuilt from scratch, which is why you can attach MDF without LDF and still get the database online. In most cases the MDF is all you need. The catch is that whether you lose data doing it depends entirely on how the database was shut down, and that is the check most guides skip.

We build SQL recovery tools at Univik and have attached and rebuilt more orphaned MDF files than we can count since 2013. The clean method below loses nothing on a database that closed properly, and the fallback for a database that did not is where the risk lives. Cleanest method first, then the fallback, with no pretending about which one costs you data.

What the LDF does and Why It can be Rebuilt

The LDF is the transaction log, the running record of every change in flight before it is fully written to the data file. SQL Server needs it on startup to finish or roll back any work that was mid transaction, which is why it refuses to attach a data file without a log to check. Our guide to the MDF file format covers how the MDF, NDF and LDF relate if you want that background.

Here is the part that makes recovery possible. When a database is closed cleanly, every transaction is already settled, committed and written or rolled back, so there is nothing left in the log that the data file does not already have. A fresh, empty log loses nothing in that case, because there was nothing outstanding for it to hold. SQL Server can build that new log and attach the MDF on its own. The whole question of data loss comes down to whether anything was still in flight when the log disappeared.

First, Check for a Clean Shutdown

This single check decides whether your recovery is clean or costs you data, and it is the step to make before running any command. A database shut down properly, through a normal SQL Server stop or a clean detach, has no open transactions, so rebuilding its log is free. A database whose log vanished during a crash, a power cut or a forced kill may have had transactions in flight, and rebuilding the log throws those away.

Clean shutdown
Normal stop or a proper detach
No transactions left in flight
Rebuild the log, lose nothing

Dirty shutdown
Crash, power cut or a forced kill
Transactions may have been mid write
Rebuild can discard uncommitted work

How the database closed decides whether rebuilding the log is free or costs you the work that was in flight.

You do not always know how the database was closed, and that is fine, because the clean method below tells you. It succeeds on a cleanly closed database and fails with a clear message on one that was not, so trying it first is safe. It never discards data on its own, it only rebuilds the log when doing so is lossless.

Attach and Rebuild the Log the Clean Way

Before you run anything, copy the MDF somewhere safe, so a failed attempt still leaves you a clean file to try again with. The right first move is then CREATE DATABASE with the FOR ATTACH_REBUILD_LOG option. You point it at the MDF, and any secondary NDF files, and SQL Server builds a new log and attaches the database.

CREATE DATABASE YourDatabase
ON (FILENAME = N'D:\Data\YourDatabase.mdf')
FOR ATTACH_REBUILD_LOG;
How the log rebuild works
Before
MDF present
LDF missing

ATTACH_REBUILD_LOG

After
MDF present
LDF rebuilt, database online

SQL Server reads the MDF, builds a brand new empty log, and brings the database online. On a clean shutdown that new log needs to hold nothing.

On a cleanly closed database this finishes in seconds with a new log and no data lost. Once it is online, run DBCC CHECKDB against the database to confirm the rebuilt log left everything consistent, since a fresh log replaces the old one without replaying it. If the database has more than one data file, list the MDF and every NDF in the ON clause, since SQL Server needs all of them to attach. The Management Studio attach dialog does the same thing through a menu, remove the missing log from the file list and it attempts the rebuild for you.

A lighter variant, plain FOR ATTACH, recreates the log on its own when a single log file is missing and the database closed cleanly. FOR ATTACH_REBUILD_LOG is the one to reach for, since it rebuilds the log across more cases. Either way, do not read a file activation failure message naming the old log as a failure, as long as it is followed by a line saying a new log file was created, the attach worked. If you meet older guides using sp_attach_single_file_db for this, it does the same job but is deprecated, so FOR ATTACH_REBUILD_LOG is the current way.

When the database was not shut down cleanly, this command stops and tells you why, with a message that the log cannot be rebuilt because there were open transactions or no checkpoint occurred. That message is not a failure of the method, it is the method protecting your data by refusing to silently discard the work that was in flight. When you see it, move to the next section.

When the Clean Attach Fails

A failed rebuild means the database was mid transaction when the log was lost, so getting it online now means accepting the loss of that unfinished work. The route through is emergency mode, the same one used to bring a suspect database back, and it ends in a repair that can discard data.

In outline, you create a placeholder database with the same name, stop the service, swap your MDF in for the placeholder’s file, and start SQL Server again. The database comes up suspect because its log does not match. From there you set it to emergency mode, put it in single user mode, and run a repair that rebuilds the log while discarding whatever it cannot reconcile.

ALTER DATABASE YourDatabase SET EMERGENCY;
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DBCC CHECKDB (YourDatabase, REPAIR_ALLOW_DATA_LOSS);
ALTER DATABASE YourDatabase SET MULTI_USER;

That REPAIR_ALLOW_DATA_LOSS step is the one to understand before you run it, because it forces the database consistent by deleting what it cannot repair. Our explainer on what REPAIR_ALLOW_DATA_LOSS really does covers the risk, and our guide on fixing a suspect database walks the emergency mode sequence in full. This route works, but it is the one that costs data, which is why the clean attach always comes first. A database that only landed in recovery pending from a missing log often comes back through the clean method without any of this.

Common Attach Errors and What They Mean

Two errors come up again and again when attaching an MDF, and each points at a different cause.

What the attach error is telling you
Error 5171
The MDF is not a primary database file. Often a secondary NDF pointed at by mistake, a corrupt header, or a version mismatch.

Error 5120
Access denied to the file. The SQL Server service account lacks permission on the MDF or its folder.

Match the error to its cause first. A 5120 is a permission fix, a 5171 points at the wrong file or a damaged one.

Error 5171, the MDF is not a primary database file. The most common innocent cause is pointing the attach at a secondary NDF instead of the primary MDF, since a database with several data files only has one primary. Confirm you are using the primary file. When you are, a 5171 usually means the MDF header is damaged, which is corruption rather than a missing log, and no attach command fixes that.

Error 5120, access denied. This one is a permission problem, not a database one. Grant the SQL Server service account full control of the folder holding the MDF, and copy the file to the default data folder if it sits somewhere SQL Server cannot reach. It attaches once it can read the file.

When the MDF Itself is Corrupt

Every method above assumes the MDF is healthy and only the log is gone. When the data file itself is damaged, a header a 5171 flagged, or corruption that survives a repair, no attach command can help, because there is no valid database for SQL Server to open. This is the point where reading the file outside SQL Server is the path left to the data.

The Univik SQL Database Recovery tool opens the MDF directly, without attaching it and without needing a log at all, reads the tables, indexes and stored procedures from the pages, and recovers them to a new database or an export. Because it never runs SQL Server’s attach or recovery, a file that will not mount can still give up its data. The free MDF Viewer shows what is readable before you commit, and when there is no backup behind any of this, our guide on recovering a SQL database without a backup covers the wider situation.

Which route fits your file
1
Healthy MDF, clean shutdown
FOR ATTACH_REBUILD_LOG. Rebuilds the log, loses nothing.
2
Healthy MDF, dirty shutdown
Emergency mode and a repair. Rebuilds the log, can discard data.
3
Corrupt MDF
No attach works. Read the file directly to salvage the data.

The state of the MDF and how it closed pick the route. Only the first one is guaranteed to keep everything.

Frequently Asked Questions

Can I Attach an MDF File Without the LDF?

Yes. Run CREATE DATABASE with FOR ATTACH_REBUILD_LOG pointing at the MDF, and SQL Server builds a new log and attaches the database. On a database that was shut down cleanly this loses nothing. The Management Studio attach dialog does the same thing when you remove the missing log from the file list.

Will I Lose Data by Rebuilding the Transaction Log?

Only if the database was not shut down cleanly. A clean shutdown leaves no open transactions, so a fresh log holds nothing the data file lacks and you lose nothing. A crash or forced shutdown may have left transactions in flight, and rebuilding the log discards that unfinished work. The clean attach refuses to run on a dirty shutdown rather than lose data silently.

What does FOR ATTACH_REBUILD_LOG Do?

It attaches a database from its data files and builds a new transaction log to replace a missing or damaged one. It works only when the database was closed cleanly, since a fresh log cannot recover transactions that were still in flight. When the database was not closed cleanly, the command fails with a message about open transactions rather than attach with lost data.

Why do I Get Error 5171 When Attaching an MDF?

Error 5171 means the file is not a valid primary database file. The common harmless cause is pointing the attach at a secondary NDF instead of the primary MDF, since only one file is primary. When you are using the right file, a 5171 usually means the MDF header is corrupt, which no attach command can fix and which needs a file level recovery tool.

Can I Attach an MDF With Memory Optimized Tables and No Log?

No, the log rebuild is not supported for a database that contains a memory optimized filegroup. FOR ATTACH_REBUILD_LOG and the emergency mode repair both refuse on such a database, so a restore from backup is the route there. This is a firm limit, not a workaround you can talk your way past.

What If the Attach Fails Because the MDF is Corrupt?

When the MDF is damaged rather than just missing its log, no attach method works, because SQL Server has no valid database to open. A tool that reads the MDF directly, such as Univik SQL Database Recovery, recovers the tables from the pages without attaching the file or needing a log. A restore from backup is cleaner when one exists, but this is the route when it does not.

The Bottom Line

Attaching an MDF without the LDF is usually simpler than it looks, because SQL Server can build a fresh log on its own. The one thing that decides your outcome is how the database was closed. On a clean shutdown, FOR ATTACH_REBUILD_LOG rebuilds the log and attaches the database with nothing lost, and it is always the first thing to try since it refuses to run when it would cost you data.

Only when that clean attach fails does the emergency mode route come in, and it trades the unfinished transactions for a working database. And when the MDF itself is corrupt, no attach can help, so reading the file directly is what salvages the data. Try the lossless method first, understand what the fallback costs, and keep tested backups so a missing log is a five minute rebuild rather than a hard decision about what to discard.

About the Author

Written and maintained by Leena Taylor Paul and the Univik team, developers of SQL database recovery and digital forensics tools since 2013. The methods here reflect real work attaching and rebuilding orphaned MDF files across SQL Server 2000 through the current versions, including files whose logs were gone and whose data files were too damaged to mount. Stuck with an MDF and no log? Talk to our team.

Last verified July 2026.