Skip to content

Instantly share code, notes, and snippets.

@ghotz
Created December 19, 2025 16:15
Show Gist options
  • Select an option

  • Save ghotz/99899709e9d36b1f0586683de277a29e to your computer and use it in GitHub Desktop.

Select an option

Save ghotz/99899709e9d36b1f0586683de277a29e to your computer and use it in GitHub Desktop.
Move all tempdb files
DECLARE @original_path sysname = N'E:\SQLServer\MSSQL15.SQL2022\MSSQL\DATA';
DECLARE @destination_path sysname = N'E:\SQLServer\MSSQL16.SQL2022\MSSQL\DATA';
DECLARE @sqlcmd nvarchar(max) = (
SELECT
N'ALTER DATABASE [tempdb]'
+ N' MODIFY FILE (NAME = [' + [name] + ']'
+ N', FILENAME = ''' + REPLACE(physical_name, @original_path, @destination_path) + ''');'
+ CHAR(10)
FROM sys.master_files
WHERE [database_id] = DB_ID('tempdb')
FOR XML PATH(''))
PRINT @sqlcmd
--EXEC(@sqlcmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment