Listing information about all database files in SQL Server

后端 未结 13 2239
遥遥无期
遥遥无期 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:34

    Executing following sql (It will only work when you don't have multiple mdf/ldf files for same database)

    SELECT
        db.name AS DBName,
        (select mf.Physical_Name FROM sys.master_files mf where mf.type_desc = 'ROWS' and db.database_id = mf.database_id ) as DataFile,
        (select mf.Physical_Name FROM sys.master_files mf where mf.type_desc = 'LOG' and db.database_id = mf.database_id ) as LogFile
    FROM sys.databases db
    

    will return this output

    DBName       DataFile                     LogFile
    --------------------------------------------------------------------------------
    master       C:\....\master.mdf           C:\....\mastlog.ldf
    tempdb       C:\....\tempdb.mdf           C:\....\templog.ldf
    model        C:\....\model.mdf            C:\....\modellog.ldf
    

    and rest of the databases

    If your TempDB's have multiple MDF's (like mine have), this script will fail. However, you can use

    WHERE db.database_id > 4
    

    at the end and it will return all databases except system databases.

提交回复
热议问题