Wednesday, March 13, 2019

Get Database Size (MB) using tSQL, SQL Server

Using below tSQL we can check the database's data/row size , log size & total size in MB,
this sql will list all databases (system & user)


------------------------------
SELECT DATABASE_NAME = s.name
    , LOG_SIZE_MB = CAST(SUM(CASE WHEN type_desc = 'LOG' THEN size END) * 8. / 1024 AS DECIMAL(10,2))
    , ROW_SIZE_MB = CAST(SUM(CASE WHEN type_desc = 'ROWS' THEN size END) * 8. / 1024 AS DECIMAL(10,2))
    , TOTAL_SIZE_MB = CAST(SUM(size) * 8. / 1024 AS DECIMAL(10,2))
FROM sys.databases s
JOIN sys.master_files m
ON s.database_id = m.database_id
GROUP BY s.name
ORDER BY s.name --SUM(size) desc
---------------

DB Size.png

No comments:

Post a Comment