Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - get all databases with MDF and LDF File Location

Tags:

I need a T-SQL query for a list of all databases in SQL Server 2008 showing

  • the name of the database and
  • the location of the .mdf and .ldf files.
like image 749
Mehdi Haghshenas Avatar asked Nov 21 '14 08:11

Mehdi Haghshenas


People also ask

Where are the MDF and LDF files stored?

Default Location of MDF File in SQL Server Files that are common and used by all instances on a single system are installed inside the folder :\Program Files\Microsoft SQL Server\nnn\.

Where is Master MDF file located?

The msdb database file (msdb. mdf) and msdb log files (msdb. ldf) are located in the Program Files\Microsoft SQL Server\Mssql\Data directory. Due to the amount of configuration information stored in the msdb database, the database should be routinely backed up.

How do I get MDF file from SQL Server?

Launch SSMS -> Connect to the SQL Server instance -> Right-click on Database -> Click Attach. In the new Locate Database Files window, browse the file system to locate the MDF file. Double-click it. The associated data files and log files are populated in the associated files grid view in the Attach Databases window.


2 Answers

SELECT     db.name AS DBName,     type_desc AS FileType,     Physical_Name AS Location FROM     sys.master_files mf INNER JOIN      sys.databases db ON db.database_id = mf.database_id 

enter image description here

like image 79
Raj Avatar answered Oct 27 '22 11:10

Raj


enter image description here

select      d.name as 'database',     mdf.physical_name as 'mdf_file',     ldf.physical_name as 'log_file' from sys.databases d inner join sys.master_files mdf on      d.database_id = mdf.database_id and mdf.[type] = 0 inner join sys.master_files ldf on      d.database_id = ldf.database_id and ldf.[type] = 1 
like image 22
Alberto Spelta Avatar answered Oct 27 '22 11:10

Alberto Spelta