Change the default data and log paths of SQL Server
Change the default folders for new data and log files of a SQL Server instance with xp_instance_regwrite.
The default locations for new data and log files are stored in the instance registry, under HKEY_LOCAL_MACHINE\Software\Microsoft\MSSQLServer\MSSQLServer. The extended stored procedure xp_instance_regwrite writes these values.
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
, N'Software\Microsoft\MSSQLServer\MSSQLServer'
, N'DefaultData'
, REG_SZ
, N'C:\SQL\Data'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE'
, N'Software\Microsoft\MSSQLServer\MSSQLServer'
, N'DefaultLog'
, REG_SZ
, N'C:\SQL\Logs'
GO
Notes
- The folders used must exist on the server and be accessible to the SQL Server service account.
- The new values apply to databases created afterwards. The files of existing databases are not moved.
xp_instance_regwriteis an undocumented extended stored procedure.