Table of Contents
    SQL Database Repair

    How to Repair SQL Server Database with a Corrupt Log File?


    Table of Contents

      Summary: Corruption in a transaction log file (.ldf) file makes the SQL Server database inaccessible. This blog outlines the possible reasons behind log file corruption and errors you may encounter because of corruption. It also describes methods to repair SQL Server database with a corrupt log file. If the log file is severely corrupted, then you can use Stellar Repair for MS SQL to repair corrupt MDF file along with LDF file with complete integrity.

      A database in SQL Server comprises three types of files: primary data file (.mdf), secondary data file (.ndf), and transaction log file or log database file (.ldf).

      The primary and secondary data files are used for storing information about database objects like tables, indexes, triggers, views, etc. On the other hand, log file records all transactions and changes made to the database by each transaction. Corruption in any of these data files or the log file can make the database inaccessible.

      In this blog, however, we will restrict our discussion on log file corruption and methods to fix a corrupt .ldf file.

      Before discussing methods to fix an SQL database with a corrupt log file, it is important to determine the reasons that led to such an issue in the first place.

      Case Study
      banner"St. Robert, MO, USA Police Department Repairs Large MS SQL Database by using Stellar Toolkit for MS SQL"

      "St. Robert, USA Police Dept. Repairs Large MS SQL Database"

      Read Case Study

      What Causes Corruption in a Transaction Log File?

      Some possible reasons that may result in log file corruption are as follows:

      • Abnormal System Shutdown: The system terminated abnormally, due to power outage or other reasons, without clean shutdown of the database.
      • Storage Size Issue: Transaction log file has limited storage space, and exceeding its limit increases chances of corruption.
      • Hardware Issue or Faulty Memory: Hardware fault occurred with the I/O subsystem used for hosting the system and database files, or faulty memory.
      • Virus Infection: The system hosting SQL server database is affected by a virus attack, resulting in a corrupt log file.

      Errors You May Encounter Due To Log File Corruption

      Error 1: Error Message 15105

      Operating system error 23 (failed to retrieve text for this error. Reason: 15105) on file "C:\Program Files\Microsoft SQL Server\MSSQL10_50.MYSERVER\MSSQL\DATA\MY_DATABASE.ldf" during CheckLogBlockReadComplete

      Error 2: File Activation Failure (Location File Error)

      File activation failure. The physical file name: C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\XXX.ldf: may be incorrect. Diagnose and correct additional errors, and retry the operation.

      Error 3: Failed to Attach Log File

      When trying to attach a log file on a new SQL Server, you may receive an error:

      :Could not open new database :Your_DatabaseName:. CREATE Database is aborted:.

      Error 4: SQL Server Error 9004

      An SQL server error 9004 occurred while processing the log for database '%.*ls'. If possible, restore from backup. If a backup is not available, it might be necessary to rebuild the log.

       Error 5: Attach Database Failed Error 1813

      Database Failed Error

      Could not open new database ?Database_Name?. CREATE DATABASE is aborted.

      Methods to Repair SQL Server Database with a Corrupt Log File

      Before applying any method, check the SQL Server error logs and event logs of the Windows system and application hosting the database. If you find any hardware problem, get it fixed and see whether the issue has been resolved or not. If not, apply the following methods to repair the damaged log file. 

      Method 1 - Restore Database from a Healthy Backup

      Note: If you don?t have a proper backup strategy configured or the backup files are lost, skip to method 2.

      The simple and safe approach to fix log file corruption issue is restoring the database from the last healthy, point-in-time, database backup.

      Let?s look at the steps to restore SQL Server database from full database backup:

      • Open SQL Server Management Studio (SSMS) and then connect to SQL Server.
      • Expand Databases node in Object Explorer.
      • Right-click the Database and select Restore Database.
      select Restore Database
      • In Restore Database window, under Source for restore section, select From device, and then click the button next to it to specify database file location.
      Restore Database window
      • In Specify backup window, select Backup media type, and then click Add button to insert the backup file location.  
      Specify backup window
      • Select the backup file you need to restore and click OK.
      • Again, click OK.
      insert the backup file location
      • The backup file will be listed on the Database restore window.
      • Before restoring the backup, select Options under Select a page section in the Restore Database window.
      • Select one of the checkboxes under Restore Options section. Click OK.

      Suggested Read - How to Restore SQL Server Database from Command Line

      Method 2 - Rebuild Corrupt Transaction Log File

      Try rebuilding the corrupt log file to make the database accessible again. To do so, follow these steps:

      • Put the database in EMERGENCY MODE by using the following command:
      ALTER DATABASE  SET EMERGENCY, SINGLE_USER
      GO

      Running the above command will help bring the database up without a transaction log file.

      Rebuild Corrupt Transaction Log File
      • Once the database is up, set it to SINGLE_USER mode, and run the DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS option to repair the database with a corrupt log file.

      Note: Before proceeding with the repair process, make sure to create backup copy of your database. This is because the REPAIR_ALLOW_DATA_LOSS option involves data loss risk.

      DBCC CHECKDB ('TestTRNLogCorrupt', REPAIR_ALLOW_DATA_LOSS)

      The above command can help you resolve log-based corruption errors, including the SQL attach error 9004, SQL Server error 1813, and more. However, it may not provide complete recovery of the severely corrupt log file.

      When you try to repair highly corrupted log file using DBCC CHECKDB, you may receive the following error:

      SQL Server error

      If corruption is still there, then use the following command to rebuild the log file. Before this, bring the database in Offline mode and change the name of the corrupted log file associated with it.

      ALTER DATABASE [original_log_file_name] REBUILD LOG ON (NAME= logicalname, FILENAME='C:\Program Files\Microsoft SQL Server\MSSQLn.MSSQLSERVER\MSSQL\DATA\file_name.ldf')

      It helps you bring the inconsistent database online by creating a new log file and discarding the old one. It can cause logical inconsistencies in the data file.

      What if the Transaction Log File is unrecoverable?

      If the transaction log file is severely corrupted or it is unrecoverable and you only have the MDF file available, then you can attach the database without the transaction log file. To do so, use the CREATE DATABASE command with FOR ATTACH_REBUILD_LOG.

      CREATE DATABASE testdb ON
      (FILENAME = ?C:\Program Files\Microsoft SQL Server..\MSSQL\DATA\testdb.mdf?)
      For ATTACH_REBUILD_LOG
      GO

      This method only applies if the MDF file is readable.

      Use Stellar Repair for MS SQL Software

      If you still cannot access your database, or if the MDF file is corrupted or damaged, using a SQL database repair tool. The software repairs the .mdf file and restores all the data to a new file.

      Steps to use Stellar Repair for MS SQL Software

      • Download, install, and launch Stellar Repair for MS SQL software.
      • The Instruction window is displayed. Follow the instructions and click OK.
      Stellar Repair for MS SQL Software
      • In Select Database dialog box, click Browse to select the affected MDF file you want to repair.

      Note: If you do not know the MDF file location, click Search option to find and select the file.

      click Search option to find and select the file
      • After selecting the file, click Repair to start the repair process.
      • The software provides a preview of the repaired database file in a tree-view list. You can see it on the left panel of Preview window.
      click Repair to start the repair process
      • Select all or specific database objects you want to recover and then click Save on the Home menu.
      • In the Save As window, select MDF under Save As section. Next, choose whether you want to save the repaired database file in New Database or Live Database.
      Save repaired database
      • Fill in the required details under Connect to Server section and then click Save.

      Conclusion

      SQL Server database transaction log file becomes corrupt due to several reasons, including unclean shutdown of the database, hardware fault, large-sized LDF file, and virus attack. A corrupt log file can make the database inaccessible.

      You can try restoring the database from the last known good backup. But, keep in mind, restoring database from backup may cause data loss depending on the recovery strategy in place.

      If the backup is not available or has failed, you can try rebuilding the corrupt log file using DBCC CHECKDB command. But this command has certain limitations. It may fail to fix a severely corrupted SQL Server database and returns an error.

      Also, running DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS option results in data loss.

      If none of the methods works, use Stellar Repair for MS SQL software to repair corrupt log file and fix SQL database issues, without data loss risk.

      No, doing a full backup does not reset the log backup.
      The DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS option checks the database for any inconsistency error. The command first attempts to use the log file to recover from any database inconsistencies. Secondly, if the log is missing it helps rebuild the transaction log file. The ALTER DATABASE REBUILD LOG command won’t work if there were open transactions (not written to disk) when the log file turns inaccessible.

      Was this article helpful?

      No NO

      About The Author

      Monika Dadool linkdin

      Monika Dadool is a Senior Content Writer at Stellar with over 5 years of experience in technical writing. She is a tech enthusiast and expert who specializes in writing about SQL Server, MySQL Server, MariaDB Server, Microsoft Access, Active Directory, email recovery, Microsoft 365, pattern recognition, machine learning, data recovery, file repair, and operating systems like Linux, Windows, and Mac. She also writes about accounting software such as QuickBooks and Sage 50, as well as web-scripting languages like HTML, JavaScript, Python, PHP, Visual Basic, ASP.NET, and AJAX. Monika is passionate about researching and exploring new technologies, and she enjoys developing engaging technical blogs that help organizations and database administrators resolve various issues. When she's not creating content, you can find her on social media, watching web series, reading books, or exploring new food recipes.

      Leave a comment

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

      Image Captcha
      Refresh Image Captcha

      Enter Captcha Here :

      Related Posts

      WHY STELLAR® IS GLOBAL LEADER

      Why Choose Stellar?

      • 0M+

        Customers

      • 0+

        Years of Excellence

      • 0+

        R&D Engineers

      • 0+

        Countries

      • 0+

        PARTNERS

      • 0+

        Awards Received

      ×