Listing information about all database files in SQL Server

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

    If you rename your Database, MS SQL Server does not rename the underlying files.

    Following query gives you the current name of the database and the Logical file name (which might be the original name of the Database when it was created) and also corresponding physical file names.

    Note: Un-comment the last line to see only the actual data files

    select  db.database_id, 
            db.name "Database Name", 
            files.name "Logical File Name",
            files.physical_name
    from    sys.master_files files 
            join sys.databases db on db.database_id = files.database_id 
    --                           and files.type_desc = 'ROWS'
    

    Reference:

    https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-master-files-transact-sql?view=sql-server-ver15

    https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-databases-transact-sql?view=sql-server-ver15

提交回复
热议问题