September 22, 2011

SQL Server: Find Database File Path

Suppose you want to find out the path where the SQL Server database files(mdf and ldf) are located, using a query. You can use system procedure sp_helpdb and system view sysfiles to obtain this information.

The following methods shows the path of the database files for master database

Method 1 : Use sp_helpdb

EXEC sp_helpdb master

db_file
Method 2 : Use sys.sysfiles view

select filename from master.sys.sysfiles

sql-db-filepath


About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at www.sqlteam.com and a moderator at www.sql-server-performance.com. His T-sql blog is at http://beyondrelational.com/blogs/madhivanan

2 comments:

Vishal said...

For SQL Server 2005+ you can also use sys.database_files:

SELECT name, physical_name
FROM sys.database_files

Suprotim Agarwal said...

I am not 100% sure but I think sys.sysfiles was either removed or planned to be removed from SQL 2005+ onwards. So yes sys.database_files is a good option.

For read-only db's in particular, I would recommend trying the master.sys.master_files DMV.