How to move SQL Server user database files from a different location?





This is a very straightforward process. Please follow below steps:
  1. 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

  1. 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.
  1. Now take the database offline using below code:
ALTER DATABASE  databasename  
SET OFFLINE
WITH ROLLBACK IMMEDIATE

  1. Physically move the files to new location 
  1. Bring the database online using below code:
ALTER DATABASE  databasename  
SET ONLINE

At this point, database is physically moved to new location.

Comments