Monday, 29 June 2026

SQLDBA - create the temp db files dynamically

 Below script will  be help full for creating temp DB files for specified count 


DECLARE @tempdbfilecount INT;

DECLARE @tempdatadir VARCHAR(255);

DECLARE @tempdatasize INT;

DECLARE @temp_data_filegrowth INT;

DECLARE @temp_log_filegrowth INT;

DECLARE @templogsize INT;


/*tempdb*/

SET @tempdatadir = 'E:\Program Files\Microsoft SQL Server\MSSQL17.SQL1\MSSQL' --Required. Please set the correct path to the local tempdb folder.

SET @tempdbfilecount = 8 --!! Required. Default setting is 8, please only change if you have a good reason!

SET @tempdatasize = 512 --Size of tempdb data files in MB

SET @temp_data_filegrowth = 256 --Size of tempdb data file autogrowth in MB


SET @templogsize = 512 --Size of tempdb log file in MB

SET @temp_log_filegrowth = 256 --Size of tempdb log file autogrowth in MB


DECLARE @tempdbfileloop INT

SET @tempdbfileloop = @tempdbfilecount


WHILE (@tempdbfileloop>0)

BEGIN

IF @tempdbfileloop=1

BEGIN

EXECUTE ('ALTER DATABASE tempdb

  MODIFY FILE 

  ( NAME = tempdev,

NEWNAME = tempdb_data' + @tempdbfileloop + ',

  FILENAME = '''+ @tempdatadir + '\tempdb_data' + @tempdbfileloop + '.mdf'',

  SIZE = ' + @tempdatasize + ' MB,

FILEGROWTH = ' + @temp_data_filegrowth + ' MB)')

END

IF @tempdbfileloop <> 1

BEGIN

EXECUTE ('ALTER DATABASE tempdb

  ADD FILE 

  ( NAME = tempdb_data' + @tempdbfileloop + ',

  FILENAME = '''+ @tempdatadir + '\tempdb_data' + @tempdbfileloop + '.ndf'',

  SIZE = ' + @tempdatasize + ' MB,

FILEGROWTH = ' + @temp_data_filegrowth + ' MB)')

END

SET @tempdbfileloop = (@tempdbfileloop-1)

END


EXECUTE ('ALTER DATABASE tempdb

  MODIFY FILE

  ( NAME = templog,

  FILENAME = '''+ @tempdatadir + '\templog.ldf'',

  SIZE = ' + @templogsize + ' MB,

FILEGROWTH = ' + @temp_log_filegrowth + ' MB)'

  );


No comments:

Post a Comment

SQLDBA : export or download SSIS packages

As part of the SSIS package upgrade planning, we need to ensure that the latest source code for all deployed packages is available in our so...