Database Size

DECLARE @dbName VARCHAR(100)
SET @dbName='DATABASE_NAME'
Select
    sd.name as DBName
    ,mf.name as FileName
    ,case mf.file_id
        when 1 then 'Main'
        when 2 then 'Log'
    END as FileType
    ,mf.file_id
    ,CONVERT(varchar(100), Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),mf.size))      * 8000)/1024)/1024)))+' MB' as SIZE
from sys.databases sd
join sys.master_files mf
on sd.database_ID = mf.database_ID
AND sd.name=@dbName
GROUP BY sd.name,mf.name,mf.database_id,mf.file_id

The above script when @dbName is set calculates the size of a database for the mdf and log file seperately.

Leave a Reply