Move TempDB database from one drive to another drive

Sometimes database administrators need to move temporary SQL Server database - TempDB, from one drive to another drive.
Maybe you decided to move TempDB database if the existing drive does not have enough space and you often get one the following errors because the disk where the tempdb files are located is full:

The log file for database 'tempdb' is full. Back up the transaction log for the database to free up some log space.

or:

Could not allocate a new page for database 'TEMPDB' because of insufficient disk space in filegroup 'DEFAULT'. Create the necessary space by dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup.

Or you want to move TempDB to another physical drive to improve disk read.

To get the names of the files used for TempDB database run this script:

USE TempDB
GO
EXEC sp_helpfile
GO

Result will be similar to this:

tempdev 1 C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\tempdb.mdf PRIMARY 8192 KB Unlimited 10% data only
templog 2 C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\templog.ldf NULL 512 KB Unlimited 10% log only

By default the names of the files are tempdev and templog.
To move mdf and ldf files run the following code:

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

The changes will be active only after the SQL Server restart, TempDB files will be created in new locations. After that you can delete the old files from the old directory.

To see how to Add or Remove Data Files to SQL Server tempdb Database, read related blog posts:
Add Data Files to SQL Server tempdb Database
How to Remove Secondary Data File from SQL Server Database