How to fix Error 5174 on Windows Server 2025 / Windows 11

Troubleshooting Guide: SQL Server Error 5174 — “Each File Size Must Be Greater Than or Equal to 512 KB”

Why This Error Happens

This error occurs when SQL Server 2025 attempts to attach, create, or restore a database file (.mdf, .ndf, or .ldf) that is either corrupted, truncated, or has a reported file size smaller than the minimum 512 KB threshold required by the Database Engine. This typically happens when a file was incompletely copied, transferred over a network with interruptions, compressed/decompressed improperly, or manually edited outside of SQL Server. It can also occur if the file header is corrupted, causing SQL Server to misread the actual file size during the attach or restore operation.

Step-by-Step Resolution Guide

Step 1: Verify the Actual File Size on Disk

  • Navigate to the file location, typically C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\.
  • Right-click the .mdf or .ldf file and check Properties > Size.
  • If the file is significantly smaller than expected (e.g., a few KB instead of several MB), the file is likely corrupted or incomplete.
  • Run dir in Command Prompt from the DATA directory to confirm the exact byte size matches SQL Server’s expectations.

Step 2: Re-copy or Re-transfer the Database File

  • If the file originated from a network share, USB drive, or cloud storage, re-copy it using a lossless transfer method.
  • Use Robocopy instead of standard copy-paste to preserve file integrity:
  • robocopy "D:\Source" "C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA" filename.mdf /Z
  • Verify checksums before and after transfer using Get-FileHash in PowerShell to confirm no data loss occurred.

Step 3: Attempt to Reattach the Database Using T-SQL

  • Open SQL Server Management Studio (SSMS) and connect to your instance.
  • Run the following command to attempt a manual attach:
  CREATE DATABASE YourDBName
  ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\YourDBFile.mdf')
  FOR ATTACH;
  • If this fails with Error 5174, the file itself is confirmed as the point of failure, not a permissions or path issue.

Step 4: Restore From a Known-Good Backup

  • If the source file cannot be repaired, locate the most recent .bak file.
  • Restore using:
  RESTORE DATABASE YourDBName
  FROM DISK = 'C:\Backups\YourDBName.bak'
  WITH RECOVERY;
  • Ensure the backup file itself is also above the 512 KB minimum and not truncated.

Step 5: Check for Antivirus or Storage Driver Interference

  • Confirm Windows Defender or third-party antivirus is not quarantining or locking the SQL data files during transfer.
  • Add an exclusion for the SQL Server DATA directory in Windows Security > Virus & Threat Protection > Exclusions.
  • Re-run the attach operation after confirming no file locks are present using Resource Monitor.

Alternative or Verification Steps

  • To verify the fix, run sp_helpdb YourDBName in SSMS and confirm the database status shows ONLINE with the correct file sizes listed under size (in 8-KB pages, so 512 KB = 64 pages minimum).
  • If the original file cannot be recovered and no backup exists, use a third-party MDF repair tool to attempt data extraction before rebuilding the database from scratch.