Listing information about all database files in SQL Server

后端 未结 13 2103
遥遥无期
遥遥无期 2021-02-01 00:08

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 th

13条回答
  •  清酒与你
    2021-02-01 00:51

    The query will error if multiple data files (e.g. ".ndf" file types) are used in one of the databases.

    Here's a version of your query using joins instead of the sub-queries.

    Cheers!

    SELECT
        db.name AS DBName,
        db.database_id,
        mfr.physical_name AS DataFile,
        mfl.physical_name AS LogFile
    FROM sys.databases db
        JOIN sys.master_files mfr ON db.database_id=mfr.database_id AND mfr.type_desc='ROWS'
        JOIN sys.master_files mfl ON db.database_id=mfl.database_id AND mfl.type_desc='LOG'
    ORDER BY db.database_id
    

提交回复
热议问题