I have inherited a SQL server 2012 instance where the file directories of the log and data files are on the same folder on the same drive(all are in the C:\ drive). My task that I will be doing is separating the data files from the OS disk. The instance already has few databases on it running.Can I just change the settings of the instance to point it to the new directory, detach the existing databases move the data files and attach it back? Please share what would be the best approach in doing this.
Sql-server – how to change the file directory of your log and data files and what are the effects
datafilemigrationsql serversql-server-2012
Related Question
- Sql-server – What are the risks of copying MDF/LDF files without detaching the DB first
- Sql-server – SQL Server data folder access rights
- Sql-server – How to move SSAS tabular instance databases
- Sql-server – Unable to backup SQL Server from a newly added clustered drive
- Sql-server – Creating/restoring mdf/ldf to non-default file location giving access denied
- Sql-server – How to change entire SQL server location from C:\ program file to another drive
Best Answer
Below are two scripts I've used for this purpose quite a few times in the past; just adjust accordingly for your environment.
Basically just plug in the new path locations, the data and log file names, and the logical SQL data and log file names, and then run the first script (see comment above second script below).
First Script
Plug in your variable accordingly for your environment.
Second Script
Before you run the below TSQL, you will need to move the applicable physical data and log files to the new locations as you plugged into the first script.