You may occasionally have the need to relocate database files. What are some reasons that may prompt the need for this type of change? First, someone might have discovered that the database files are on the same drive as the operating system. For a discussion of why you don’t want to do this see this post. Second, your database might simply be outgrowing its current drive. Third, perhaps someone installed the transaction log file on the same drive as the mdf file.
So, after the need to do this has arisen, how is the task actually done? The first step, which may have already been done as part of a process that discovered the issue in the first place, is to confirm the current location of your database files. There are at least two ways to approach this. First, if you want to look at all the information on the SQL Server instance then use the below query that utilizes sys.masterfiles.
SELECT MF.name AS LogicalFileName , physical_name AS FilePath FROM sys.master_files AS MF
A second way to do this is to review the information for a single database. For that, the query below can be used. Note I am looking for AdventureWorks2008R2 in my particular query.
SELECT D.name AS DBName , MF.name AS LogicalFileName , MF.physical_name AS FilePath FROM sys.master_files AS MF INNER JOIN sys.databases AS D ON MF.database_id = D.database_id WHERE D.name = 'AdventureWorks2008R2'
Click to enlarge.
Once this is complete the next step is to verify that the target drive for the new file is large enough to hold the drive that you plan on moving. You really don’t want to get in the middle of this change only to discover that the target drive does not have enough room to hold the file you want to move. In this case I will be demonstrating how to move a transaction log file and so I will use a DBCC command to return space information.
The DBCC command gives you the current size of the log in megabytes, as well as other useful information like the percentage of space used in that log file. In this case my transaction log file is very small at 3.36 MB for the AdventureWorks2008R2 database.
Another way to see this information for a specific database would be to right click the database name in SQL Server Management Studio and select Tasks > Shrink > Files. Switch the display to the Log file and the dialog will show the currently allocated space for the transaction log file as well as the free space in that file at the moment.
Once you have confirmed that the target drive will hold the file you want to use the next step will be to take the database offline so that the file you want to move can be copied to the target drive. Use the script below to offline your database, replacing AdventureWorks2008R2 with the name of your database.
ALTER DATABASE AdventureWorks2008R2 SET OFFLINE WITH ROLLBACK IMMEDIATE;
Now using Windows Explorer, copy the file from its current location to the new location and run the below query. for this demonstration I simply made a new folder called Logs in the default path like this: ‘C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Logs
ALTER DATABASE AdventureWorks2008R2 MODIFY FILE (NAME = AdventureWorks2008R2_log, FileName = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Logs\AdventureWorks2008R2_log.ldf');
Then bring the database online.
ALTER DATABASE AdventureWorks2008R2 SET ONLINE;
That’s it. The files have been successfully moved. Congratulations! Performance has been improved or a disaster with a full drive has been averted.