Wednesday, March 7, 2012

Retrieve the default data folder path

Is it possible to find out the default path SQL Server creates databases in
depending on what instance you are connected to. E.g. My default instance
default data path is C:\Program Files\Microsoft SQL Server\MSSQL\Data. My
named instance path I also have is C:\Program Files\Microsoft SQL
Server\MSSQL$Testing\Data and my SQL Express data path is C:\Program
Files\Microsoft SQL Server\MSSQL.3\MSSQL\Data.
Can I find out the default data path for the current instance I am connected
to using a query?
ThanksYou can probably use the Model database's SysDatabases.FileName to get the
path.
"Chris" wrote:

> Is it possible to find out the default path SQL Server creates databases i
n
> depending on what instance you are connected to. E.g. My default instance
> default data path is C:\Program Files\Microsoft SQL Server\MSSQL\Data. My
> named instance path I also have is C:\Program Files\Microsoft SQL
> Server\MSSQL$Testing\Data and my SQL Express data path is C:\Program
> Files\Microsoft SQL Server\MSSQL.3\MSSQL\Data.
> Can I find out the default data path for the current instance I am connect
ed
> to using a query?
> Thanks
>
>|||Thanks Mike,
If I do the following it seems to return the path.
SELECT REPLACE(filename, 'model.mdf', '') AS DataPath FROM SysDatabases
WHERE name = 'model'
Chris
"Mike K" <MikeK@.discussions.microsoft.com> wrote in message
news:E3722206-05B2-4026-9E59-9F863C75B73E@.microsoft.com...
> You can probably use the Model database's SysDatabases.FileName to get the
> path.
> "Chris" wrote:
>|||You might want to research what database you are currently connected to.
Just checking a different database might not get you really what you want.
set nocount on
create table #output
(
spid int,
status varchar(100),
login varchar(100),
hostname varchar(100),
blkby varchar(10),
dbname varchar(30),
command varchar(50),
cputime int,
diskio int,
lastbatch varchar(20),
programname varchar(200),
spid2 int
)
insert into #output exec sp_who2
set nocount off
select
ltrim(rtrim(dbname))
from
#output
where
spid = @.@.spid
drop table #output

No comments:

Post a Comment