An alternative to sp_spaceused

The built-in procedure sp_spaceused can provide some useful information, especially when an object name is specified; great for showing the finer details of that object.

For example the MSreplication_options table:

exec sp_spaceused 'MSreplication_options'

The information for the number of rows and especially index size can be useful when copying the data or determining space requirements when rebuilding indexes, respectively.

And when an object is not specified, information about the entire database is shown, providing some good information. For example:

exec sp_spaceused

Personally, I like to see more file information and get more granular details about a database and its size compared to the traditional, sp_spaceused.

Yes, Microsoft has made some improvements to sp_spaceused in recent versions of SQL Server, however, I like to see the white space and a little more detail about each of the database files belonging to a database, not just the overall available space.

Knowing where my allocated space resides will help me plan for growth. So without further adieu here is the script.

SET NOCOUNT ON;
SELECT DB_NAME() AS DBName,
[name] AS [FileName],
file_id AS ID,
state_desc AS FileStatus,
type_desc AS FileType,
size / 128.0 AS FileSizeMB,
FILEPROPERTY(name, 'SpaceUsed') / 128.0 AS SpaceUsedMB,
size/128.0 - CAST(FILEPROPERTY([name], 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB,
physical_name,
CASE is_read_only
WHEN 0 THEN 'FALSE'
WHEN 1 THEN 'TRUE'
END as is_read_only
FROM sys.database_files;

And here is some sample output of what this script will show you:

Now, if you wanted to add more detail to what is already shown, you could look at the contents of sp_spaceused by doing the following…

Enjoy the script and hopefully it assists you in your day-to-day DBA functions as it does myself.

Edited January 12, 2016

I’ve modified the script to include 2 additional columns, one for used space and one for free space of each file as a percentage, which, at-a-glance allows one to see any problem areas quicker. The new script is as follows: