Showing posts with label SQL Data path. Show all posts
Showing posts with label SQL Data path. Show all posts

Thursday, February 26, 2009

How to change the default database storage location in SQL?

"How do I set the default database file path"I keep seeing this question in many forums. When you fire "CREATE DATABASE " T-SQL command without any parameters. The database will get created and its file will be located in some path, lets say "C:\Program Files\MS SQL\Data". But if you want to have the files located else where, lets assume "D:\SQL_Data\" then there are two options that you can think of.
1 - specify the file path in the "CREATE DATABASE " command or
2- set the default path location in server configuration.
For option one refere to BOL, here is how to workout Option-II: again there are two ways to do this task.
one, using GUI (Management Studio/Enterprise manager) - right click on server node, select properties from pop-up menu, look for "Database Settings" in that section there two boxes - one for LOG and other for DATA file. Key in the appropriate values and save it. This change will effect after stop & start SQL server.
The other way to achieve this configuration change is using T-SQL, as show below,
USE [master]
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData', REG_SZ, N'D:\SQL_Data'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultLog', REG_SZ, N'D:\SQL_Data'
GO
Yes, its instance specific registery entry that it takes to set this property. Here the command is using extended stored procedure "xp_instance_regwrite" to do the trick. This type of command can be used in cases where you want to standardise you SQL Server setup and you may want to add this step as a post installation procedure/batch file etc.