SQL Server 2008: How to query all databases sizes?

后端 未结 15 947
伪装坚强ぢ
伪装坚强ぢ 2021-01-29 18:08

I have MS SQL 2008 R2, 500 databases. What is the most efficient, easiest and \'modern\' way to query all databases sizes.

The output should have columns:

15条回答
  •  北海茫月
    2021-01-29 19:07

    The following code worked for me very well.

        SELECT
            D.name As DbName,
            F.Name AS FullDbName,
            CASE WHEN F.type_desc='ROWS' THEN 'mdf' ELSE 'ldf' END AS FileType,
            F.physical_name AS PhysicalFile,
            CONVERT(DATE,D.create_date) AS CreationDate,
            F.state_desc AS OnlineStatus,
            CAST((F.size*8)/1024 AS VARCHAR(26)) + ' MB' AS FileSize_MB,
            CAST(F.size*8 AS VARCHAR(32)) + ' Bytes' AS FileSize_Bytes,
            CAST(CAST(ROUND((F.size*8)/(1024.0*1024.0),0) AS INT) AS VARCHAR(32)) + ' GB' AS FileSize_GB
    
        FROM 
            sys.master_files F
            INNER JOIN sys.databases D ON D.database_id = F.database_id
    
        ORDER BY
             D.name 
    

提交回复
热议问题