views:

312

answers:

6

We have a few huge databases (20GB+) which mostly contain static lookup data. Because our application executes joins against tables in these databases, they have to be part of each developers local SQL Server (i.e. they can't be hosted on a central, shared database server).

We plan on copying a canonical set of the actual SQL Server database files (*.mdf and *.ldf) and attach them to each developer's local database.

What's the best way to find out the local SQL Server instance's data directory so we can copy the files to the right place? This will be done via an automated process, so I have to be able to find and use it from a build script.

A: 

In enterprise manager, right click on the database and choose properties. Then select "Files" and you should see the data files there.

Matt Wrock
A: 

From the GUI: open your server properties, go to Database Settings, and see Database default locations.

Note that you can drop your database files wherever you like, though it seems cleaner to keep them in the default directory.

Michael Petrotta
+1  A: 

select physical_name from sys.database_files;

Remus Rusanu
A: 

Small nitpick: there is no data folder, only a default data folder.

Anyway, to find it, assuming you want to install for the first default instance:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\Setup\SQLDataRoot

If there's a named instance, MSSQL.1 becomes something like MSSQL10.INSTANCENAME.

Stu
This requires that I know the SQL Server instance name (e.g. MSSQL.1, MSSQL.2, MSSQL._N_). How do I get that instance name?
splattered bits
For default instances, it's not the name -- it's just the number. Which instance to install on is up to you! To find out which instances are installed, look at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL
Stu
I need to attach my databases in the same instance where the other application instances are located. Essentially, I would need to convert the friendly instance name (the one that shows up in SQL Server Enterprise Manager, and which is the same across all machines) to this internal, "numeric" identifier.
splattered bits
So look in Instance Names, find the instance name, look up the identifier (i.e. the value), then go back and look in the appropriate key.
Stu
+1  A: 

I stumbled across this solution in the documentation for the Create Database statement in the help for SQL Server:

SELECT SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
                  FROM master.sys.master_files
                  WHERE database_id = 1 AND file_id = 1
splattered bits
This was the quickest solution to implement. One step.
splattered bits
+1  A: 

Various components of SQL Server (Data, Logs, SSAS, SSIS, etc) have a default directory. The setting for this can be found in the registry. Read more here:

http://technet.microsoft.com/en-us/library/ms143547%28SQL.90%29.aspx

So if you created a database using just CREATE DATABASE MyDatabaseName it would be created at the path specified in one of the settings above.

Now, if the admin / installer changed the default path, then the default path for the instance is stored in the registry at

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\[INSTANCENAME]\Setup

If you know the name of the instance then you can query the registry. This example is SQL 2008 specific - let me know if you need the SQL2005 path as well.

DECLARE @regvalue varchar(100)

EXEC master.dbo.xp_regread @rootkey='HKEY_LOCAL_MACHINE',
     @key='SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.MSSQLServer\Setup',
     @value_name='SQLDataRoot',
     @value=@regvalue OUTPUT,
     @output = 'no_output'

SELECT @regvalue as DataAndLogFilePath

Each database can be created overriding the server setting in a it's own location when you issue the CREATE DATABASE DBName statement with the appropriate parameters. You can find that out by executing sp_helpdb

exec sp_helpdb 'DBName'
Raj More
This doesn't help because the database I would be querying doesn't exist yet. Is there an equivalent stored procedure for instances (as opposed to databases)?
splattered bits
answer edited based on comments
Raj More