SQL Server Log Explorer
SQL Server Error Error 9004

How to Fix SQL Server Error 9004 When the Log is Damaged

Andrew Jackson Modified: 9 Minutes Reading
On this page

Quick Answer

SQL Server error 9004 means the transaction log is damaged. Check the state number in the error log first. You get 3 fixes in this guide. Restore from a backup first. Rebuild the log only when no backup exists. Scan the MDF file with recovery software when both fail. DBCC CHECKDB cannot see log damage. Copy the MDF and LDF files before any repair.

Error 9004 stops recovery, restore and some ALTER DATABASE commands. Severity 21 also disconnects your session. Microsoft states the error means the contents of the transaction log are damaged.

This guide shows how to find the state number, how to test your backups and which of the 3 fixes fits your case. Our own tests ran on SQL Server 2022 and the results sit next to the methods.

What does SQL Server Error 9004 Look Like?

Error 9004 shows up in the SQL Server error log and in the Windows Application event log as Event ID 9004. Microsoft documents this text.

Error: 9004, Severity: 21, State: 1.
An error occurred while processing the log for database 'mydb'.
If possible, restore from backup. If a backup is not available,
it might be necessary to rebuild the log.

Microsoft states error 9004 appears next to error 3414 in most cases. Its symbolic name is LOG_CORRUPT.

Which State Number Points to Which Damage?

You find the state number in the error text. It tells you where SQL Server found the problem.

State Microsoft meaning What to do first
1 The header of a virtual log file is damaged. This differs from the file header of the LDF file Check for Msg 5172 in the error log. It points to the LDF header. Then restore a backup
2 and 3 Log content was invalid while a RESTORE performed recovery The backup is damaged. Try the previous backup set
4 to 12 Checks on the log failed. Microsoft names parity, sector and other logical checks Check the disk and the Windows System event log for storage errors

Microsoft states the causes match any other database corruption problem. Your storage path is the first suspect.

Where did You See the Error?

Where it appeared What it means What to do
Error log at instance startup Recovery of the database failed on a damaged log Restore a backup with Method #1
During RESTORE LOG or RESTORE DATABASE The log part of that backup file is damaged Try an earlier backup set with Method #1
After ALTER DATABASE or another command The command needed the log and hit the damage. Severity 21 closed your session Check the state number and the disk. Then use Method #1 or Method #2
STANDBY restore on a log shipping secondary Microsoft links error 9004 to STANDBY restores on SQL Server 2014 to 2017 builds in KB4058700 and KB2987585 Restore the log backups without the STANDBY option and update the build
Restore of a compressed backup from many files on a large TDE database Microsoft lists this as a SQL Server 2016 bug in KB4025628 with a fix in Cumulative Update 7 Update SQL Server 2016 to that build or later

How do You Test the Log Before You Repair Anything?

Do these 3 checks first. They tell you if a good backup exists and what the log looks like now.

Search the error log for the error. Results show the state number and the time.

EXEC sys.sp_readerrorlog 0, 1, N'9004';

Verify your backups with CHECKSUM. SQL Server answers with a valid message when it can open the backup set. Test the last full backup and every log backup after it.

RESTORE VERIFYONLY FROM DISK = N'D:\Backups\YourDB_full.bak' WITH CHECKSUM;
RESTORE VERIFYONLY FROM DISK = N'D:\Backups\YourDB_log.trn' WITH CHECKSUM;

List the virtual log files. This view works while the database is online.

SELECT vlf_sequence_number, vlf_size_mb, vlf_active, vlf_status
FROM sys.dm_db_log_info(DB_ID(N'YourDatabase'));

Our test executed both VERIFYONLY statements on a full backup and a log backup. SQL Server answered that the backup set on file 1 is valid for both. Our VLF query returned all 4 columns for a healthy log.

Heads Up

Microsoft states DBCC CHECKDB does not check the transaction log. Even a clean CHECKDB result does not rule out error 9004.

What do the Fixes Need on Your Server?

System Specifications

Component Requirement
Server permission sysadmin role for ALTER DATABASE and DBCC CHECKDB repair. We executed every test as sysadmin
Disk space Free space for a copy of the MDF and LDF files before Method #2
Backup location A volume that stores your last full backup and every log backup after it
PC for the recovery software Windows up to Windows 10 with 512 MB of RAM and 50 MB of free disk space

Software Specifications

Item Requirement
SQL Server version Microsoft lists the 9004 page for SQL Server 2017 and later. We tested on SQL Server 2022 (RTM CU26) Express on Linux. The recovery software lists SQL Server 2019 and earlier
Test database Healthy restored copies
Client tool SQL Server Management Studio or sqlcmd

How do You Fix SQL Server Error 9004?

Work from the safest fix down. Copy the MDF and LDF files to another volume before Method #2.

Method #1: Restore From a Known Good Backup (Free, No Repair Risk)

Microsoft lists a backup restore as the first action. Restore the last full backup with NORECOVERY and apply the log backups in order.

RESTORE DATABASE [YourDB] FROM DISK = N'D:\Backups\YourDB_full.bak'
WITH REPLACE, NORECOVERY;
RESTORE LOG [YourDB] FROM DISK = N'D:\Backups\YourDB_log.trn'
WITH RECOVERY;

Our test executed this full and log restore pattern on a copy of a test database. Add a tail log backup first when the log still opens. Our tail log backup guide explains it and our restore full database backup guide shows the SSMS route.

Error 9004 during the restore means the log part of that backup file is damaged. Move to the previous backup set and test it with VERIFYONLY.

Method #2: Rebuild the Log With DBCC CHECKDB (Free, Data Loss Possible)

Use this method when no usable backup exists. Microsoft states the rebuild can lose transactional consistency and gives no full ACID guarantee.

ALTER DATABASE [YourDB] SET EMERGENCY;
ALTER DATABASE [YourDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DBCC CHECKDB (N'YourDB', REPAIR_ALLOW_DATA_LOSS) WITH NO_INFOMSGS;
ALTER DATABASE [YourDB] SET MULTI_USER;

Our test showed a rule that older guides omit. In EMERGENCY mode our database accepted only REPAIR_ALLOW_DATA_LOSS. Both REPAIR_REBUILD and REPAIR_FAST returned Msg 7901 with the text that this level of repair is not supported in emergency mode.

  • Check the result. Microsoft states the database can remain inconsistent after a log rebuild. Execute DBCC CHECKCONSTRAINTS and compare your row counts with your last report.
  • Take a new full backup at once. It gives you a clean restore point.
  • Copy the files first. Repair edits the files in place and you cannot undo it.

Heads Up

Our statements executed on a healthy restored copy. Our copy came back ONLINE in SINGLE_USER mode and we changed it to MULTI_USER.

Healthy copies prove the syntax and do not prove a repair on a real damaged log. Our DBCC CHECKDB repair failed guide shows the next step when this method fails.

Method #3: Scan the MDF File With SQL Recovery Utility (Paid, No Backup Needed)

Use this method when no backup exists and Method #2 fails or you cannot risk the files. SQL Recovery Utility opens the MDF or NDF file without a live SQL Server. The software shows a preview of all database items and exports to a live SQL Server database, a SQL script or CSV files.

  • Click Open and select the .mdf file of the database.
  • Select Quick Scan or Advanced Scan and tick Auto detect SQL version.
  • Check the preview of the recovered items.
  • Select SQL Server Database, enter the login details and select a new or an existing database.
  • Select With only schema or With schema and data and click Export.

Open button of the recovery software for a damaged database file
Quick Scan and Advanced Scan options of the recovery software
Preview of recovered database objects in the recovery software

SQL Recovery Utility

Scan a damaged MDF file and export the recovered data.

Heads Up

Scan a copy of the MDF file and work on the copy only.

How do You Prevent Log Damage?

  • Take log backups with CHECKSUM. SQL Server stores a checksum in the backup and VERIFYONLY tests it later.
  • Test a restore. VERIFYONLY proves the backup opens and a real restore to a copy proves the data.
  • Watch the storage path. Microsoft names the same causes as general corruption. Check the Windows System event log after every unclean shutdown.
  • Schedule DBCC CHECKDB and remember its limit. Microsoft states it ignores the log. Backups with CHECKSUM close that gap.

FAQs

Does Error 9004 Mean the Database is Corrupt?

Not always. Error 9004 means the transaction log is damaged and the data files can remain healthy. Check them with DBCC CHECKDB after you restore or rebuild the log.

Can DBCC CHECKDB Find a Damaged Transaction Log?

No. Microsoft states DBCC CHECKDB does not check the transaction log. Even a clean result does not rule out error 9004.

What is the Difference Between Error 9004 and Error 3414?

Error 9004 names the damaged log. Error 3414 reports that recovery failed. Microsoft states both appear together in most cases.

Why does Error 9004 Appear When You Restore a Log Backup?

That backup file has a damaged log part. Microsoft ties states 2 and 3 to invalid log content during a RESTORE. Restore an earlier backup set.

Can You Fix Error 9004 Without a Backup?

Yes. Rebuild the log with Method #2 or scan the MDF file with Method #3. Method #2 can lose transactional consistency.

Did Microsoft Fix Error 9004 in a Newer Build?

Only for the cases in KB4058700, KB2987585 and KB4025628. Those notes name STANDBY restores on SQL Server 2014 to 2017 and TDE restores on SQL Server 2016. Our STANDBY restore on SQL Server 2022 finished with no error 9004.

About this guide

We checked this guide against Microsoft documentation on 30 Nov 2026 for SQL Server 2017 to 2025. We executed the VERIFYONLY statements, the VLF query, a STANDBY restore and the emergency repair statements on our test server.

Microsoft documents the error on its MSSQLSERVER_9004 page and the repair options on its DBCC CHECKDB page.