How to Change tempdb Location in SQL Server

Changing tempDB location from the C drive to another drive or location in SQL Server 2022 or any SQL version

Here, in our case, from C Drive to D drive

Solution:

Step 1: script out – script

To move the location of the tempdb database, we must make a script with all logical names and locations for all files. Thanks to BrentOzar for his time-saving script, where we should only change the new location.

In our case, it is D drive

SELECT 'ALTER DATABASE tempdb MODIFY FILE (NAME = [' + f.name + '],'
	+ ' FILENAME = ''D:\MSSQL\tempDB\' + f.name
	+ CASE WHEN f.type = 1 THEN '.ldf' ELSE '.mdf' END
	+ ''');'
FROM sys.master_files f
WHERE f.database_id = DB_ID(N'tempdb');

Step 2: Change to the required location

Step 3: exec

Step 4: Re-start the SQL services

Step 5: Check the new location & delete the old files

Leave a Reply