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

Share with
Tags
Latest Comments
By Mark Gentry on Windows Server 2019 - Change product key does nothing
20 Aug 2021, 03:30 AM
By Cathy on In-Place Upgrade for a Windows Server domain controller
31 Jul 2021, 18:28 PM
By Mr. Greymatter on Raspberry Pi - Running Java app on Raspbian
16 Feb 2021, 07:35 AM
By Mikko Seittenranta on Xamarin Forms multiple instances of same app open
16 Feb 2021, 04:34 AM
By Andrew on Auto/Custom height on Xamarin Forms WebView for Android and iOS
22 Jan 2021, 22:15 PM
By Nick on Raspberry Pi - Running Java app on Raspbian
14 Oct 2020, 19:37 PM
By Ivan on Fixed: Value cannot be null Parameter Name: source
15 Sep 2020, 19:47 PM
By Anand on Raspberry Pi - Bluetooth using Bluecove on Raspbian
7 Sep 2020, 16:53 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development