0

Is there a way to find the physical path of SQL Server file (.mdf)? I have VS2010 where I browse the SQL Server database in Server Explorer window. I do not have sql Server Management studio.

Thanks in advance

1
  • why do you need that ? Commented Jul 11, 2013 at 14:39

1 Answer 1

2

This gives the database where name = 'master'. Just change this to the database you're looking for.

SELECT name, physical_name AS current_file_location
FROM sys.master_files
WHERE name = 'master'

You can also try:

SELECT filename FROM sys.sysfiles

This gives the default locations

 DECLARE @defaultDataLocation nvarchar(4000)
DECLARE @defaultLogLocation nvarchar(4000)

EXEC master.dbo.xp_instance_regread
    N'HKEY_LOCAL_MACHINE',
    N'Software\Microsoft\MSSQLServer\MSSQLServer',
    N'DefaultData', 
    @defaultDataLocation OUTPUT

EXEC master.dbo.xp_instance_regread
    N'HKEY_LOCAL_MACHINE',
    N'Software\Microsoft\MSSQLServer\MSSQLServer',
    N'DefaultLog', 
    @defaultLogLocation OUTPUT

SELECT @defaultDataLocation,    @defaultLogLocation
Sign up to request clarification or add additional context in comments.

2 Comments

if you are in the database try: SELECT * FROM sysfiles
The second part of the accepted answer (registry read) worked for me to give the DEFAULT location of user databases - this is exactly what I was looking for! Excellent, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.