Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove (merge) SQL Servers' database secondary data file

I have a database backup for which SQL Server Management Studio says that it has three files in it: an .mdf file, an .ndf file and one .ldf file. This secondary data file (the .ndf one) was created for no obvious reason, so I want to remove it altogether (without losing data, of course), preferably during while the database is being restored from the backup.

Is this at all doable?

like image 836
Anton Gogolev Avatar asked Feb 02 '10 11:02

Anton Gogolev


People also ask

Can we DELETE NDF file?

NDF files are just like MDF files, they contain data. LDF files are for log data. Do NOT delete NDF files!

Can I DELETE SQL Server transaction log file?

To delete data or log files from a databaseExpand Databases, right-click the database from which to delete the file, and then click Properties. Select the Files page. In the Database files grid, select the file to delete and then click Remove. Click OK.

How do I DELETE a SQL Server NDF file?

You can use DBCC SHRINKFILE with the EMPTY FILE option which will migrate the data to all of the other files in the same filegroup. After this completes no data should be in the secondary file and you can use an alter database to remove the file.


1 Answers

Ok, found a solution.

First back up the database.

Execute this:

USE database_name;

Then execute this, and replace logical_ndf_file_name with the logical name of your NDF file (which you can easily find out via Database->Properties_Files):

DBCC SHRINKFILE('logical_ndf_file_name', EMPTYFILE);
ALTER DATABASE database_name REMOVE FILE logical_ndf_file_name;
like image 91
Anton Gogolev Avatar answered Sep 20 '22 12:09

Anton Gogolev