How do I find the data directory for a SQL Server instance?

后端 未结 15 2603
挽巷
挽巷 2020-11-30 23:16

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 t

15条回答
  •  借酒劲吻你
    2020-11-30 23:58

    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'
    

提交回复
热议问题