A tempdb can have multiple data files. Let's learn how to add and remove tempdb data files.
A few days ago, I was working on tempDB contentions and how to resolve them. In this article, I will explain, how to resolve errors encountered when adding and deleting multiple tempDB data files.
To determine how many tempDB files exist, run the following query in SQL Server Management Studio.
- use tempDB
- go
- EXEC SP_HELPFILE;
To add a data file in atempDB, use the following query:
- ALTER DATABASE tempdb
- ADD FILE (NAME = tempdev2, FILENAME = 'W:\tempdb2.mdf', SIZE = 256);
- use tempDB
- go
- EXEC SP_HELPFILE;
Restart SQL Server Instances and then execute the following query.
- USE tempdb;
- GO
- DBCC SHRINKFILE('tempdev2', EMPTYFILE)
- GO
- USE master;
- GO
- ALTER DATABASE tempdb
- REMOVE FILE tempdev2;
If the error still exists then modify the tempdev2 file by reducing its size to only 1 MB.
To modify a temp db size run the following query:
- ALTER DATABASE [tempdb] MODIFY FILE (
- NAME = N'tempdev2',
- SIZE = 1024KB );
- USE tempdb;
- GO
- DBCC SHRINKFILE('tempdev2', EMPTYFILE)
- GO
- USE master;
- GO
- ALTER DATABASE tempdb
- REMOVE FILE tempdev2;
This will remove the tempdev2 data file.
In this article, you learned how to add and delete tempdb data files.

Comments
Join the conversation! Your thoughts help the community grow.