Microsoft SQL: Get freespace in database file

Microsoft SQL allocate space in database files even if there is no data. To see how much data there is in a database you can query database.

SELECT DB_NAME() AS DbName, 
    name AS FileName, 
    type_desc,
    size/128.0 AS CurrentSizeMB,  
    size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB
FROM sys.database_files
WHERE type IN (0,1);

Leave a Reply

Your email address will not be published.