Moving the tempdb database

By Bill Graziano on 05 November 2007 | 3 Comments | Tags: Administration


Moving the tempdb database in SQL Server is a simple process but does require the service to be restarted.

By default, the tempdb database is created in the same location as master, model and msdb.  tempdb is used to store temporary user created objects (e.g. temp tables), temporary internal objects (e.g. work tables for sorting) and any row version data.  Each time SQL Server is started all the objects in tempdb are deleted but the file sizes are retained.  People primarily want to move tempdb to increase performance or deal with file size issues.

The first step is to determine the logical file names of of the data files for tempdb.  You can run the following command in SQL Server 2000 or SQL Server 2005:

USE tempdb
GO
EXEC sp_helpfile
GO


name      fileid filename                      
--------- ------ -------------------------------
tempdev        1 c:\Data\MSSQL\data\tempdb.mdf 
templog        2 c:\Data\MSSQL\data\templog.ldf

I've omitted some extra columns from the result set.  The logical names always seem to be tempdev and templog.  Knowing this we can run the following ALTER DATABASE statement in SQL Server 2000 or SQL Server 2005 to move tempdb.  Set the FILENAME parameter to the location where you'd like each file.

USE master
GO
ALTER DATABASE tempdb 
	MODIFY FILE (NAME = tempdev, FILENAME = 'C:\tempdb2005.mdf')
GO
ALTER DATABASE tempdb 
	MODIFY FILE (NAME = templog, FILENAME = 'C:\tempdb2005.ldf')
GO

SQL Server will return a message indicating that the old files can be deleted after restarting SQL Server or that the new files will be available after the database is restarted depending on which version of SQL Server you're using.  In either case, you'll need to restart SQL Server for the change to take effect.

Discuss this article: 3 Comments so far. Print this Article. This page has been read 6,537 times.

If you like this article you can sign up for our newsletter. We send it out each week that we post a new article. There's an opt-out link at the bottom of each newsletter so it's easy to unsubscribe at any time.

Email Address:

Related Articles

Using xp_ReadErrorLog in SQL Server 2005 (12 May 2008)

Centralized Asynchronous Auditing across Instances and Servers with Service Broker (20 August 2007)

Centralized Asynchronous Auditing with Service Broker (16 July 2007)

SQL Server 2005 Best Practices on TechNet (4 December 2006)

Scripting Database Objects using SMO (Updated) (29 November 2005)

Troubleshooting Performance Problems in SQL Server 2005 (3 November 2005)

Dynamic Management Views (30 October 2005)

Using SHERLOCK to Monitor Blocking (2 August 2005)

Other Recent Forum Posts

Group by (12 Replies)

Transfer logins from SQL Server 2000 to 2005 (0 Replies)

Backing up SQL db to a remote machine (0 Replies)

Simple question (connect to database) (3 Replies)

null values (1 Reply)

Linked Server - Excel Spreadsheet (3 Replies)

sp (4 Replies)

Sql Server Business Intelligence development studi (0 Replies)

Subscribe to SQLTeam.com

Weekly SQL Server newsletter with articles, forum posts, and blog posts via email:

SQLTeam.com Articles via RSS

SQLTeam.com Weblog via RSS

- Advertisement -

SQL Server Jobs