SQL Server Admin
T-SQL Articles

November 11, 2009

Programmatically Change the default path for SQL Server Database creation




The extended stored procedure "xp_instance_regwrite" helps you read/write from the registry. You can use this stored procedure to programmatically change the path where the data files for a new SQL Server database gets created by default. Here’s how to use this stored procedure

USE [master]
GO
EXEC xp_instance_regwrite
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer',
N'DefaultData',
REG_SZ,
N'D:\MyDatabases'
GO

EXEC xp_instance_regwrite
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer',
N'DefaultLog',
REG_SZ,
N'D:\MyDatabases'
GO

Note: After running this query, a folder called ‘MyDatabases’ should exist in D drive before a new database is created, else you will get an error.


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

2 Responses to "Programmatically Change the default path for SQL Server Database creation"
  1. Anonymous said...
    December 23, 2009 3:39 AM

    didn't work dude........am using sql server 2008 and no 'mydatabases' folder created in 'd:'.. :-(

  2. Anonymous said...
    January 11, 2010 1:29 AM

    I think it's the wording of the poster's comments. The implication is that you need to create the folder, or you'll get an error.

 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions