SQL Server Combine MDF Files – The Easy Way

Do you need to combine or merge two data files (MDF or NDF)?  There are a number of complicated options available or you can use the two lines of code below to do the trick... It's up to you.  This method works with both SQL Server 2005 and SQL Server 2008.

USE DataBaseName
GO

DBCC SHRINKFILE(LogicalNameOfFileToRemove, EMPTYFILE)
ALTER DATABASE DataBaseName REMOVE FILE LogicalNameOfFileToRemove

If you don't know the logical name of your MS SQL Server files use the following T-SQL query:

SELECT
name AS LogicalName
,sys.database_files.physical_name AS PhysicalName
FROM
sys.database_files

By Phil Steffek

Phil Steffek is a professional sumo wrestler who is hoping to represent the U.S. in the next Olympics. During the off season Phil is a Data Architect working on transactional databases, data warehouse implementations, and all aspects of business intelligence. Also, Phil is not really a sumo wrestler. You can get to know a bit more about my non-sumo doings by checking out my LinkedIn Page

Leave a comment

Your email address will not be published.