How to Attach MDF Data File Without LDF Log File – Interview Question of the Week #078

Question: Can you attach an MDF data file when the LDF log file is missing?

Answer: Sometimes. SQL Server can rebuild a missing log for a read/write database that shut down cleanly and has all its data files available. An MDF alone is not a guarantee that attachment or recovery will succeed.

A grounded blue kite with a new cord and an old detached cord

During a performance-tuning exercise, I was explaining why the log file matters. One DBA asked what happens if the LDF is lost or corrupted and they no longer have a useful log file. It is a good interview question because the short answer can easily become dangerous advice.

Keep the original files intact and work on copies. If there is a sound backup, restore from it. Do not delete a present log in the hope that SQL Server will manufacture a better one. An unclean shutdown, missing secondary data file, read-only database or inaccessible file can prevent the rebuild. Rebuilding the log also breaks the existing log-backup chain, so take a new full backup afterward.

The T-SQL method

For a suitable cleanly shut-down database, this template makes the rebuild intent explicit. Replace the database name and file paths:

-- Recovery/attach template only, using copies of cleanly shut-down files.
USE master;
CREATE DATABASE [DatabaseWithoutLogFile]
ON (FILENAME = N'D:\DATA\DatabaseWithoutLogFile.mdf')
FOR ATTACH_REBUILD_LOG;
-- Include every required NDF data file when its path differs.
-- Take a new full database backup after a successful log rebuild.

The original example used FOR ATTACH. That can also rebuild a single missing log when its documented conditions are met. Its original screenshot is worth keeping because the “File activation failure” line is followed by confirmation that SQL Server created a new log. Read the entire message, not just its first line:

Historical FOR ATTACH messages followed by confirmation that a new log was created

The SSMS method

Current SSMS Attach Databases dialog with Add and Associated Files controls before selecting an MDF
Current Attach Databases dialog. No file has been selected or attached in this view.

In current SSMS, the Attach Databases dialog has an Add control for selecting the MDF and an Associated Files list for checking the required files. The image below shows the empty dialog before any file is selected; it does not show a recovery being performed.

  1. In Object Explorer, right-click Databases and choose Attach.
  2. Click Add and select the MDF file. Ensure that the complete required data-file set is available.
  3. In Database details, select the unavailable log entry and remove that entry from the dialog. This does not mean deleting a physical log file.
  4. Click OK only after checking the files and recovery conditions. Review all messages and verify the attached database.

Historical Object Explorer showing the successfully attached database

If attachment fails, preserve the messages and files. A failure may mean the database needs recovery that a fresh empty log cannot provide. The transaction log records work required for consistency; it is not a disposable companion to the MDF.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Backup and Restore, SQL Scripts, SQL Server, SQL Server Management Studio, Transaction Log
Previous Post
Moving TempDB to New Drive – Interview Question of the Week #077
Next Post
How to Send Execution Plan in Email? – Interview Question of the Week #079

Related Posts

10 Comments. Leave new

  • Thanks Pinal. Even though I was aware of the SSMS methods, I was unaware of the T-SQL method.

    Reply
  • First time commenter, long time fan.
    It’s important to note that this only work if the database was properly set offline before detaching, so it will not work if, say, you lost the ldf by removing a drive unexpectedly.

    Miguel

    Reply
  • You can always click the “Script” button in SSMS to see the T-SQL that’s going to be executed

    Reply
  • What additional steps are needed to have the new LDF created n the server non default location for log files? I mean if you want to spcify a different path for the new LDF that will be created. Thanks.

    Reply
  • You are the best, Thank you

    Reply
  • sir i am trying to attached a database but error is popping up like

    database “xyz” cannot be open due to inaccessible or memory space…

    but i have lot of space on my drive…
    Please suggest some thing to resolved this issue…

    Reply
  • abhishek thakur
    August 21, 2018 11:37 am

    in my Prod scenario database size is 300MB and ldf file size is 100GB.
    1) database recovery model is full.
    2)we cant take transactional back.
    3) is there any way to reclaim for 100GB.

    if i use the detached and attached the mdf file with remove ldf file option.is there any datalosse.

    kindly suggest.

    Reply
  • Thank you for this. What can you do when the database was not shut down properly and there is not a recent backup? The MDF was moved to new location and attached for ATTACH_FORCE_REBUILD_LOG. But when the command is executed, it is still wanting the LDF from the original path. Its not rebuilding a new one.

    Msg 5120, Level 16, State 101, Line 8
    Unable to open the physical file “F:\path_to_original_log\Original_log.ldf”. Operating system error 5: “5(Access is denied.)”.
    File activation failure. The physical file name “F:\path_to_original_log\Original_log.ldf” may be incorrect.
    The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.
    Msg 1813, Level 16, State 2, Line 8
    Could not open new database ‘newDatabase’. CREATE DATABASE is aborted.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.