Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing information about all database files in SQL Server

Is it possible to list information about the files (MDF/LDF) of all databases on an SQL Server?

I'd like to get a list showing which database is using what files on the local disk.

What I tried:

  • exec sp_databases all databases
  • select * from sys.databases shows a lot of information about each database - but unfortunately it doesn't show the files used by each database.
  • select * from sys.database_files shows the mdf/ldf files of the master database - but not the other databases
like image 630
M4N Avatar asked Mar 09 '12 07:03

M4N


People also ask

How do I find all database files in SQL Server?

You have two native options for finding out where the SQL server stores its database files: either right-click on the instance name in SQL Server Management Studio (SSMS) and navigate to the 'Database Settings' tab, or use a T-SQL query.

What is the query to list all the databases?

To do that we will be using the below given commands: CREATE TABLE [database_name.] table_name ( pk_column data_type PRIMARY KEY, column_1 data_type NOT NULL, column_2 data_type, ..., table_constraints ); If we do not mention the name of the database then the default USE database is selected for the creation of tables.

How do I view a SQL Server database file?

If you ever need to know where your database files are located, run the following T-SQL code: USE master; SELECT name 'Logical Name', physical_name 'File Location' FROM sys. master_files; This will return a list of all data files and log files for the SQL Server instance.


1 Answers

You can use sys.master_files.

Contains a row per file of a database as stored in the master database. This is a single, system-wide view.

like image 200
Mikael Eriksson Avatar answered Nov 12 '22 00:11

Mikael Eriksson