SQL Server 2008: How to query all databases sizes?

后端 未结 15 949
伪装坚强ぢ
伪装坚强ぢ 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 18:51

    A better and quite simpler one

    SELECT [Database Name] = DB_NAME(database_id),
         [Type] = CASE WHEN Type_Desc = 'ROWS' THEN 'Data File(s)'
                   WHEN Type_Desc = 'LOG'  THEN 'Log File(s)'
                   ELSE Type_Desc END,
         [Size in MB] = CAST( ((SUM(Size)* 8) / 1024.0) AS DECIMAL(18,2) )
    FROM   sys.master_files
    --Uncomment if you need to query for a particular database
    --WHERE      database_id = DB_ID(‘Database Name’) 
    GROUP BY  GROUPING SETS
            (
                   (DB_NAME(database_id), Type_Desc),
                   (DB_NAME(database_id))
            ) ORDER BY      DB_NAME(database_id), Type_Desc DESC
    

    It will give you size of Data File(s) and Log File(s) separately like below

    DatabaseName    Type            Size in MB
    -------------------------------------------
    FMS             Data File(s)    23.00
    FMS             Log File(s)     1.50
    PointOfSale     Data File(s)    4.00
    PointOfSale     Log File(s)     1.25
    Union2          Data File(s)    336.00
    Union2          Log File(s)     1191.13
    SurveyProject   Data File(s)    4.00
    SurveyProject   Log File(s)     1.00
    

提交回复
热议问题