SQL

SQL Server NDF Recovery in 5 Ways

10 min read

SQL Server NDF recovery starts with the state of the missing file. Microsoft states that the database stays offline when a file of the primary filegroup is not online.

Five ways follow, from the safest to the last resort. Check the file state first because the state decides which way applies.

Quick SummarySQL Server NDF recovery depends on the state of the file and the filegroup that owns it. Check the state in sys.master_files before you touch anything. Fix the path when the file exists under another folder or name. Restore the file from backup together with the tail of the log. Restore the full chain under a new name when the file restore fails. Set the file offline and copy the healthy data when no backup exists. Use SQL Recovery when a damaged NDF file has no backup.

What Happens When an NDF File Goes Missing?

An NDF file is a secondary data file. The MDF file stores the system tables and the NDF files store extra data in filegroups.

Microsoft documents three rules on its page about file states. Every file of a filegroup must be online before the filegroup works. An OFFLINE file returns to ONLINE only through a restore from backup. The database stays offline when a file of the primary filegroup is not online.

Tables in an unavailable filegroup fail on SELECT when the heap or the clustered index sits there. Insert, update and delete statements fail on any table with an index in that filegroup.

Do You Know?Error 945 reports a database that cannot open because of inaccessible files. Check the SQL Server error log for the file name and follow SQL Server error 945 when the log shows that message.

How to Check the State of Your NDF File?

Query sys.master_files for the state of every file and sys.databases for the state of the database. Neither query modifies the database.

SELECT name, physical_name, state_desc, type_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'SalesDB');

SELECT name, state_desc
FROM sys.databases
WHERE name = N'SalesDB';

Note the logical name, the physical path and the state of the missing file. Methods 2 and 3 need the logical name and Method 4 needs the filegroup.

List the tables that live in every filegroup with the next query.

SELECT t.name AS table_name, fg.name AS filegroup_name
FROM sys.tables AS t
JOIN sys.indexes AS i ON i.object_id = t.object_id AND i.index_id IN (0, 1)
JOIN sys.filegroups AS fg ON fg.data_space_id = i.data_space_id
ORDER BY fg.name, t.name;

The query lists tables that do not use partitions. Partitioned tables need sys.partitions and sys.allocation_units.

Which Recovery Method Fits Your File State?

Match what you see to the table before you start.

What you see Start with
The file still exists on disk under another path or name Method 1
A full backup and the log backups exist and the recovery model is full or bulk logged Method 2
The file restore fails or the log backup chain has a gap Method 3
No backup exists and the other filegroups are healthy Method 4
No backup exists and the NDF file is damaged Method 5

How to Recover a SQL Database With a Missing NDF File?

Every example uses the database SalesDB and the file SalesDB_Data2. Replace the names and the paths with your own values.

Method 1: Point the Database to the Right Path

Use this method when the file still exists on disk. A new disk letter, a restored volume or a renamed folder creates this case.

Microsoft documents this order for moved files. Set the database offline, place the file in the new folder, point the metadata to the new path and set the database online.

ALTER DATABASE SalesDB SET OFFLINE;

-- Copy the NDF file to E:\Data in Windows Explorer

ALTER DATABASE SalesDB
MODIFY FILE (NAME = SalesDB_Data2, FILENAME = N'E:\Data\SalesDB_Data2.ndf');

ALTER DATABASE SalesDB SET ONLINE;

Copy the file and retain the original until the database opens. Method 1 fails for a file in the OFFLINE state because Microsoft permits only a restore for that state.

Heads UpNever detach the database to fix a missing file. A later attach needs every MDF and NDF file, so the attach fails while a file is missing.

Method 2: Restore the File From Backup

Use this method when a full backup and the log backups exist. A file restore brings back one file and leaves the rest of the database untouched.

Take a backup of the tail of the log first. NO_TRUNCATE tries the backup even when the database is damaged.

BACKUP LOG SalesDB
TO DISK = N'D:\Backup\SalesDB_tail.trn'
WITH NO_TRUNCATE;

RESTORE DATABASE SalesDB
FILE = N'SalesDB_Data2'
FROM DISK = N'D:\Backup\SalesDB_full.bak'
WITH NORECOVERY;

RESTORE LOG SalesDB
FROM DISK = N'D:\Backup\SalesDB_log1.trn'
WITH NORECOVERY;

RESTORE LOG SalesDB
FROM DISK = N'D:\Backup\SalesDB_tail.trn'
WITH RECOVERY;

Apply every log backup in order between the file backup and the tail. Apply the differential backup of the file before the log backups when one exists. Restore a database with NORECOVERY explains why the first statements use NORECOVERY.

Microsoft documents online restore for Enterprise edition and offline restore for every edition. Under the SIMPLE recovery model the file restore works only for a READ_ONLY filegroup.

Method 3: Restore the Full Chain Under a New Name

Use this method when the file restore fails or the chain has a gap. The copy needs free disk space for the full database and works under every recovery model.

RESTORE FILELISTONLY
FROM DISK = N'D:\Backup\SalesDB_full.bak';

RESTORE DATABASE SalesDB_Copy
FROM DISK = N'D:\Backup\SalesDB_full.bak'
WITH MOVE N'SalesDB' TO N'E:\Data\SalesDB_Copy.mdf',
     MOVE N'SalesDB_Data2' TO N'E:\Data\SalesDB_Copy_2.ndf',
     MOVE N'SalesDB_log' TO N'F:\Log\SalesDB_Copy.ldf',
     NORECOVERY;

RESTORE LOG SalesDB_Copy
FROM DISK = N'D:\Backup\SalesDB_tail.trn'
WITH RECOVERY;

RESTORE FILELISTONLY shows the logical names for the MOVE clauses. Restore the differential and every log backup before the tail when they exist.

Check the copy and repoint your application to it. Follow fix a SQL database stuck in restoring state when the copy stays in the Restoring state.

Method 4: Set the File Offline and Copy the Healthy Data

Use this method when no backup exists and the other filegroups are healthy. Microsoft states that OFFLINE makes all objects in the filegroup inaccessible and that the option fits only a file that can be restored later.

ALTER DATABASE SalesDB
MODIFY FILE (NAME = SalesDB_Data2, OFFLINE);

SELECT * INTO SalesCopy.dbo.Customers
FROM SalesDB.dbo.Customers;

Copy the tables from the healthy filegroups to a new database with SELECT INTO or with the Import and Export Wizard. The tables in the missing filegroup remain unreachable and the data in the missing file remains lost.

REMOVE FILE does not solve the case. Microsoft states that the file must be empty before SQL Server removes it.

Method 5: Repair the NDF File With SQL Recovery

Use this method when no backup exists and the NDF file is damaged but present. Repair a corrupted MDF file in SQL Server shows the manual DBCC options for the MDF side.

SQL Recovery opens the MDF and NDF files without SQL Server and works on a copy so your original file remains untouched. SQL Recovery previews tables, views, triggers, rules, functions and stored procedures and exports them to a SQL Server database, SQL scripts or CSV files.

Download SQL Recovery Buy SQL Recovery

  1. Install SQL Recovery and select Add File.

Add file option in SQL Recovery

  1. Browse to the MDF file and select Advanced Scan. Confirm the SQL Server version and tick the deleted records option.

Advanced Scan and SQL Server version options in SQL Recovery

  1. Open the preview and check the recovered database components.

Preview of recovered SQL database objects in SQL Recovery

  1. Select Export and save the objects to a SQL Server database, to SQL scripts or to CSV files.

Export options in SQL Recovery

Execute the next statement on the new database after the export and fix the errors it reports.

DBCC CHECKDB (N'SalesDB_Recovered') WITH ALL_ERRORMSGS, NO_INFOMSGS;

What Are the Software and System Specifications of SQL Recovery?

Check both tables before you install SQL Recovery on the server or on a separate workstation.

Software Specifications

Attribute Detail
Software name SQL Recovery
Version 14.0
File types MDF and NDF
SQL Server versions 2025, 2022, 2019, 2017, 2016, 2014, 2012, 2008 R2, 2008, 2005 and 2000
Scan modes Quick Scan and Advanced Scan
Export options SQL Server database, SQL scripts and CSV
SQL Server installation Needed only for export to a live server
Demo limit Preview of every item and export of 25 records per table and 5 other objects

System Specifications

Component Requirement
Processor Intel Core 2 Duo E4600 at 2.40 GHz or equivalent
RAM 4 GB
Hard disk space 100 MB free
Operating system Windows 7, 8 and 10 (32 or 64 bit), Windows 11 (64 bit), Windows Server 2008, 2012 R2, 2016, 2019, 2022 and 2025
Framework Microsoft .NET Framework 4.5 or later
Permission Launch SQL Recovery as administrator

Common Questions on NDF Recovery

Can You Attach a Database When an NDF File Is Missing?

No. Microsoft states that FOR ATTACH needs every MDF and NDF file. Never detach a database that has a missing file.

Can You Remove a Missing NDF File From the Database?

No. Microsoft states that REMOVE FILE works only on an empty file. The data in a missing file has no way to move out first.

Does a Missing NDF File Always Take the Database Offline?

No. The filegroup decides. The database stays offline when the missing file belongs to the primary filegroup. Tables in a secondary filegroup fail while any file of that filegroup is offline.

Does a File Restore Need a Log Backup?

Yes under the full or bulk logged recovery model. Microsoft states that you take a backup of the tail of the log before the file restore and apply the log backups after it.

Which Editions Restore a File While the Database Remains Online?

Online restore needs Enterprise edition. Offline restore works on every edition. The database or the filegroup is unavailable during an offline restore.

Can SQL Recovery Bring Back Data From a Missing NDF File?

No. Data that lived only in a missing file has no source. SQL Recovery opens the MDF and NDF files you have and repairs a damaged NDF file. Restore the file from backup when the file is gone.

How to Protect Your NDF Files Next Time?

Take a backup of every filegroup and a log backup on a schedule. Test one file restore on a copy so the steps are ready before the day you need them.

Store the path of every file in your documentation. A moved volume then costs minutes and not hours.

Who, How and WhyAndrew Jackson, Technical Content Specialist, wrote this guide. The file states, the filegroup rules, the piecemeal restore rules and the file restore rules follow the Microsoft Learn page on file states, the page on piecemeal restores and the page on restoring files and filegroups. The attach rule follows the Microsoft CREATE DATABASE page. The OFFLINE, REMOVE FILE and FILENAME rules follow the Microsoft ALTER DATABASE file options page. The T-SQL was checked for syntax against Microsoft documentation and was not executed on a live server. The SQL Recovery steps and tables follow the screens and the product page of the software. The guide did not test a missing NDF file with SQL Recovery. The guide exists because a missing NDF file leaves several states and every state needs a different way.

Key Takeaways

  • SQL Server NDF recovery starts with the state of the file in sys.master_files.
  • Microsoft states that an OFFLINE file returns online only through a restore from backup.
  • The database stays offline when the missing file belongs to the primary filegroup.
  • A file restore needs the tail of the log under the full and bulk logged recovery models.
  • FOR ATTACH needs every MDF and NDF file so never detach a database with a missing file.
  • SQL Recovery repairs a damaged NDF file when no backup exists.