Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SQL Data File Path

Author  Topic 

scootermcfly
Yak Posting Veteran

66 Posts

Posted - 2003-04-16 : 17:21:01
I would like to get the default directory where the data files are stored and I can't seem to find anything in BOL about this. Is there an easy way to get this? Or should I just parse the path from the FileName field in the sysfiles table?

Thanks,
Scooter McFly


kp_kumar
Starting Member

1 Post

Posted - 2003-04-16 : 18:41:46
see sp_MSget_setup_paths procedure , from master db

kris

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-04-16 : 18:43:47
Well the database files do not have to be located in the default data directory, but here is how to find out where the default data directory exists:

For the primary instance:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSSQLServer\Setup]
"SQLDataRoot"

For a named instance:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\<Instance Name>\Setup]
"SQLDataRoot"

I don't know why they are in different locations.

To get the information using T-SQL, you'll want to call xp_regread (example is for primary instance):

DECLARE @regvalue varchar(100)

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

PRINT @regvalue

The value will not include the data directory. So for example, if your default data directory is E:\MSSQL\Data, @regvalue will give you E:\MSSQL.

HTH,

Tara
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-04-16 : 18:44:54
quote:

see sp_MSget_setup_paths procedure , from master db



Hmmm, that stored procedure doesn't give me any output. It just says that it ran successfully.

[EDIT]Nevermind, I get values now. I was being stupid for a second[/EDIT]

DECLARE @sql_path NVARCHAR(260)
DECLARE @data_path NVARCHAR(260)

EXEC master.dbo.sp_MSget_setup_paths @sql_path OUTPUT, @data_path OUTPUT

PRINT @data_Path

It turns out that sp_MSget_setup_paths does pretty much the same as my code. (I looked at the code in the stored proc)

Tara


Edited by - tduggan on 04/16/2003 18:54:31

Edited by - tduggan on 04/16/2003 18:55:00
Go to Top of Page

scootermcfly
Yak Posting Veteran

66 Posts

Posted - 2003-04-17 : 09:09:01
Thanks all, I think I might just read from the sysfiles table for right now. My problem is that I want to create a filegroup and file on a client's server and I am trying to create a script that will do this, but I need to have the path info to use in the ALTER DATABASE ADD FILE statement.

So this is what I am using to find out the physical path to the default data file in the database where this will run:

Select Reverse(substring(ltrim(reverse([FileName])),charindex('\',Ltrim(reverse([Filename]))),datalength(Ltrim(reverse([Filename]))))) AS DataFilePath
From sysfiles Where groupid = 1

Scooter McFly


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-04-17 : 12:12:17
You shouldn't use system tables in any scripts especially installation scripts. MS says never to do this because the structure and name could change in the next version, however the stored procedures that do the same work will seldom change. Even though you're not thinking about the next version yet since it isn't even out, you shouldn't write scripts that might not port to the new version.

Just a suggestion of course.

Tara
Go to Top of Page

gvphubli
Yak Posting Veteran

54 Posts

Posted - 2008-12-12 : 17:23:34
Here is the article on the same.

http://gvphubli.blogspot.com/2008/04/default-database-file-path.html


TechnologyYogi
http://gvphubli.blogspot.com/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-12 : 17:33:49
gvphubli, do not spam your blog here. It is fine to post it to answer current questions, but do not reopen threads that are old such as this one that is 5 years old. I will lock your account if this continues.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -