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 Richardcax on MVC forms in Umbraco
16 Aug 2018, 00:30 AM
By Cary on MVC forms in Umbraco
12 Aug 2018, 02:58 AM
By Naufil on How to open a modal window for a custom datatype in the Umbraco back office
30 Jul 2018, 12:40 PM
By Trevor on Replacing the Xamarin Header/Navigation bar with a custom view/template
19 Jul 2018, 18:36 PM
By Luke on Setting up tables in SQLite with a primary key on a Xamarin Forms app
11 Jul 2018, 09:18 AM
By Travis on Setting up tables in SQLite with a primary key on a Xamarin Forms app
7 Jul 2018, 15:44 PM
By Luke on Auto/Custom height on Xamarin Forms WebView for Android and iOS
15 Jun 2018, 09:43 AM
By vikki on Auto/Custom height on Xamarin Forms WebView for Android and iOS
14 Jun 2018, 13:35 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development