How to move SQL Server user database files from a different location?
This is a very straightforward process. Please follow below steps:
- Execute sp_helpdb on the database (which needs to be moved) to find the logical file names and complete path of all the files associated with the database..
sp_helpdb databasename -- databasename is the database which will be moved
- Once you identified all the logical files and physical location of the all log and data files, update their new location in system catalog using below code:
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name, FILENAME = 'new_path\os_file_name' )
Note: You have to execute this code seperately for each data/log files you will move.
- Now take the database offline using below code:
ALTER DATABASE databasename
SET OFFLINE
WITH ROLLBACK IMMEDIATE
- Physically move the files to new location
- Bring the database online using below code:
SET ONLINE
At this point, database is physically moved to new location.
At this point, database is physically moved to new location.
Comments