Skip to content

Instantly share code, notes, and snippets.

@KavenTheriault
Created January 5, 2018 20:26
Show Gist options
  • Save KavenTheriault/ccfb77b919694ff8f8ad8d57f980e613 to your computer and use it in GitHub Desktop.
Save KavenTheriault/ccfb77b919694ff8f8ad8d57f980e613 to your computer and use it in GitHub Desktop.
Move SQL Server database files

Move SQL Server database files

1- Set the database offline

ALTER DATABASE DatabaseName SET OFFLINE WITH ROLLBACK IMMEDIATE;

2- Now you can move the files manualy

3- Set the new files location

ALTER DATABASE DatabaseName MODIFY FILE ( NAME = DatabaseName, FILENAME = 'D:\Database\DatabaseName.mdf' );
ALTER DATABASE DatabaseName MODIFY FILE ( NAME = DatabaseName_log, FILENAME = 'D:\Database\DatabaseName_log.ldf' );

4- Set the database online

ALTER DATABASE DatabaseName SET ONLINE;

You can validate the files location with the following query

SELECT name, physical_name AS CurrentLocation, state_desc  
FROM sys.master_files  
WHERE database_id = DB_ID(N'DatabaseName');  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment