Wednesday, August 1, 2012

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 .mdffile, 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.



First back up the database.
Execute this:
USE database_name;
Then execute this:
DBCC SHRINKFILE('ndf_file_name', EMPTYFILE);
ALTER DATABASE database_name REMOVE FILE logical_ndf_file_name;

No comments: