Moving SQL Server System Tables

Moving SQL Server System Tables sounds like it's going to be a complicated and fairly long-winded task, but with a few SQL queries, you're good to go.

Pre-requisites:

Before you start, make sure that the user you've used to connect to your instance of MSSQL Server has the correct permissions to perform administrative tasks. You'll need to have a user that's part of the sysadmin group. I also assume that you're using SQL Server Management Studio, which can be obtained at the following address:

https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms

Queries for moving the system databases:

To find out the logical names of all the database files for a database, you'll need to run the following query:

SELECT name, physical_name AS CurrentLocation, state_desc  
    FROM sys.master_files  
    WHERE database_id = DB_ID(N'tempdb');  

 The above query will display a row for each file that the database is attached to and a logical name for each one.

Note: You can change the query to use another database by replacing 'tempdb' with the database name.

The below example displays what was displayed to me when I ran the query for the master database:

name CurrentLocation state_desc
master C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\master.mdf ONLINE
masterlog C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\mastlog.ldf ONLINE

You can run the following command to move a database file location:

USE master;  
GO  
ALTER DATABASE databasename
MODIFY FILE (NAME = logicalname, FILENAME = 'F:\myfolder\databasefilename.mdf');  
GO  

Here's an example to move the tempdb 'database' files:

USE master;  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = tempdev, FILENAME = 'F:\TempDB\tempdb.mdf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = templog, FILENAME = 'F:\TempDB\templog.ldf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = temp2, FILENAME = 'F:\TempDB\tempdb_mssql_2.ndf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = temp3, FILENAME = 'F:\TempDB\tempdb_mssql_3.ndf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = temp4, FILENAME = 'F:\TempDB\tempdb_mssql_4.ndf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = temp5, FILENAME = 'F:\TempDB\tempdb_mssql_5.ndf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = temp6, FILENAME = 'F:\TempDB\tempdb_mssql_6.ndf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = temp7, FILENAME = 'F:\TempDB\tempdb_mssql_7.ndf');  
GO  
ALTER DATABASE tempdb   
MODIFY FILE (NAME = temp8, FILENAME = 'F:\TempDB\tempdb_mssql_8.ndf');  
GO  

Once you've run the command, restart the SQL Server instance to make it use the newly stored configuration

Note: The tempdb database does not require you to move any files, but other databases do, so make sure you do that before restarting the SQL Server instance.

Issues that might occur:

If you've moved the 'msdb' database and restarted the SQL Server instance, you might run into an issue where the database displays '(recovery mode)' after the database name, in this case, you need to make sure that you've manually moved the database files from 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA' or wherever your SQL Server instance has been installed, to the new folder that you specified above. You also need to make sure that Network Service has full access to the aforementioned folder.

Reasons for moving database files:

You may have your system set up to use solid SATA drives and wish to extend drive lifetime by reducing writes, in this case, moving the 'tempdb' files to a hard drive is a good idea.

You may want to store database files on another drive to save on disk space on drives that have little remaining space.

Other Resources:

Microsoft documentation for moving system databases:
https://docs.microsoft.com/en-us/sql/relational-databases/databases/move-system-databases

Microsoft documentation for the msdb database:
https://docs.microsoft.com/en-us/sql/relational-databases/databases/msdb-database


Published at

Tags: SQL,MSSQL

Luke Alderton

Comments

Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By Helenpaups on MVC forms in Umbraco
23 May 2018, 16:48 PM
By Helenpaups on MVC forms in Umbraco
23 May 2018, 13:46 PM
By AkikoTax on MVC forms in Umbraco
20 May 2018, 06:41 AM
By Klaus on MVC forms in Umbraco
17 May 2018, 10:47 AM
By Archie on MVC forms in Umbraco
16 May 2018, 18:52 PM
By w on JQuery Unobtrusive Validation for dynamically created elements
11 May 2018, 02:00 AM
By Luke on Umbraco using Global.asax to handle events before the Request Handler.
27 Apr 2018, 18:28 PM
By Janine Gardiola on Umbraco using Global.asax to handle events before the Request Handler.
26 Apr 2018, 11:18 AM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development