Listing information about all database files in SQL Server

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

    0 讨论(0)
  • 2021-02-01 00:44

    I am using script to get empty space in each file:

    Create Table ##temp
    (
        DatabaseName sysname,
        Name sysname,
        physical_name nvarchar(500),
        size decimal (18,2),
        FreeSpace decimal (18,2)
    )   
    Exec sp_msforeachdb '
    Use [?];
    Insert Into ##temp (DatabaseName, Name, physical_name, Size, FreeSpace)
        Select DB_NAME() AS [DatabaseName], Name,  physical_name,
        Cast(Cast(Round(cast(size as decimal) * 8.0/1024.0,2) as decimal(18,2)) as nvarchar) Size,
        Cast(Cast(Round(cast(size as decimal) * 8.0/1024.0,2) as decimal(18,2)) -
            Cast(FILEPROPERTY(name, ''SpaceUsed'') * 8.0/1024.0 as decimal(18,2)) as nvarchar) As FreeSpace
        From sys.database_files
    '
    Select * From ##temp
    drop table ##temp
    

    Size is expressed in KB.

    0 讨论(0)
  • 2021-02-01 00:48

    If you want get location of Database you can check Get All DBs Location.
    you can use sys.master_files for get location of db and sys.databse to get db name

    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
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-01 00:55

    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.

    0 讨论(0)
  • 2021-02-01 00:55

    You can also try this.

     select db_name(dbid) dbname, filename from sys.sysaltfiles
    
    0 讨论(0)
提交回复
热议问题