At some point you will need to move a SQL Server database to a different drive, whether that is to free up space on an overloaded volume, migrate to faster storage, or reorganise after a server rebuild. SQL Server lets you do this cleanly with ALTER DATABASE, without detaching or restoring from backup.
By the end of this post you will have moved both the data file (.mdf) and the log file (.ldf) to a new location and verified the result, all without touching SQL Server Configuration Manager.
1 Prerequisites
Before starting, make sure you have:
- A SQL Server instance with sysadmin or dbcreator rights.
- The destination folder already created on disk with the SQL Server service account granted full control.
- The physical
.mdf and .ldf files manually copied to the new location before bringing the database back online.
Important: ALTER DATABASE ... MODIFY FILE updates the path recorded in SQL Server's metadata. It does not move the files on disk. You must copy the files yourself while the database is offline, then set it back online.
2 Take the database offline
Taking the database offline flushes all pending writes, closes open connections, and releases the file locks SQL Server holds on the .mdf and .ldf files. You cannot copy a locked file, so this step must come first.
ALTER DATABASE MyNewDatabase
SET OFFLINE;
GO
Once this completes without error, you can safely copy the files at the OS level to E:\New_location\ or wherever your target path is.
3 Update the file paths in metadata
With the files copied, tell SQL Server where to find them. Run one MODIFY FILE statement per file, referencing the logical name of each file. You can find the logical names in sys.master_files if you are unsure.
ALTER DATABASE MyNewDatabase MODIFY FILE (
NAME = MyNewDatabase_Data
,FILENAME = 'E:\New_location\MyNewDatabase_Data.mdf'
);
GO
ALTER DATABASE MyNewDatabase MODIFY FILE (
NAME = MyNewDatabase_Log
,FILENAME = 'E:\New_location\MyNewDatabase_Log.ldf'
);
GO
SQL Server will confirm each change with a message along the lines of File 'MyNewDatabase_Data' modified in the system catalog. The new path will be used the next time the database is started. That message is expected and correct.
4 Bring the database back online
With the metadata updated and the files in place at the new path, set the database back online. SQL Server will open the files from the new location.
ALTER DATABASE MyNewDatabase
SET ONLINE;
GO
Tip: If SET ONLINE fails with a file-not-found error, the most common cause is that the files were not copied before this step, or the SQL Server service account does not have read and write permission on the destination folder.
5 Verify the new location
Confirm that SQL Server is using the new paths and that the database is online before considering the job done.
-- Check whether the database is moved to your desired location
SELECT name
,physical_name AS NewLocation
,state_desc AS OnlineStatus
FROM sys.master_files
WHERE database_id = DB_ID(N'MyNewDatabase')
GO
You should see both rows pointing to E:\New_location\ with an OnlineStatus of ONLINE. If either file still shows the old path, the MODIFY FILE statement for that file did not execute successfully.
Wrapping up
Moving SQL Server database files is a four-step operation: take the database offline, copy the files at the OS level, update the metadata with ALTER DATABASE MODIFY FILE, then bring the database back online. The verification query against sys.master_files gives you a definitive confirmation that the move completed as expected.
If you need to do this across several databases at once, the same pattern can be wrapped in a cursor or applied through a scripted approach using sys.master_files to generate the statements dynamically.
Got a question or ran into a problem? Drop a comment below and I will reply.