Technical Article

Check and Set Default file Locations with TSQL

,

Checks the default file locations for your data and log files. If not set, it sets them to the specified values. This is written for a SQL 2005 default instance. Registry paths may be different for SQL 2000 or named instances.

The script can be easily modified to update it if the current value is set but does not match the new value.

Declare@DataDir nvarchar(4000),
@LogDir nvarchar(4000)
@DefaultDataDir nvarchar(4000),
@DefaultLogDir nvarchar(4000)

Set @DefaultDataDir = 'D:\SQL_DATA'
Set @DefaultLogDir = 'E:\SQL_LOG'

Exec xp_instance_regread N'HKEY_LOCAL_MACHINE', 
 N'Software\Microsoft\MSSQLServer\MSSQLServer', 
 N'DefaultData', 
 @DataDir output, 
 'no_output'

Exec xp_instance_regread N'HKEY_LOCAL_MACHINE', 
 N'Software\Microsoft\MSSQLServer\MSSQLServer', 
 N'DefaultLog', 
 @LogDir output, 
 'no_output'

Select @DataDir, @LogDir

If IsNull(@DataDir, '') <> @DefaultDataDir
  Begin
Exec xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData', REG_SZ, @DefaultDataDir
  End

If IsNull(@LogDir, '') <> @DefaultLogDir
  Begin
Exec xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultLog', REG_SZ, @DefaultDataDir
  End

Rate

4.5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

4.5 (2)

You rated this post out of 5. Change rating