This post contains a short SQL Server query to get storage info for the disks hosting your instance’s database files without having to RDP into its host VM.
The query
1 2 3 4 5 6 7 8 9 | SELECT DISTINCT [vs].[volume_mount_point] AS [Disk], CAST([vs].[total_bytes] / 1024.0 / 1024.0 / 1024.0 AS DECIMAL(23, 2)) AS [DiskSize(GB)], CAST(( [vs].[available_bytes] / 1048576.0 ) / 1024.0 AS DECIMAL(23, 2)) AS [DiskFreeSpace(GB)], CAST(CAST( (([vs].[available_bytes] / 1048576.0 ) / ( [vs].[total_bytes] / 1024.0 / 1024.0 )) * 100 AS DECIMAL(23, 2)) AS NVARCHAR(25)) + N'%' AS [DiskFreePercent] FROM sys.[master_files] AS [mf] CROSS APPLY sys.dm_os_volume_stats([mf].[database_id], [mf].[file_id]) AS [vs] ORDER BY [DiskFreePercent] ASC; |
Since it relies on the dm_os_volume_stats DMV, you’ll need VIEW SERVER STATE permission on SQL Server 2019 or VIEW SERVER PERFORMANCE STATE on 2022.
Output example
The column names are self-explanatory so there’s no need for me to go over them.
Note that this will only return information about disks that have database files “living” on them.
If you want to get the free space for all the drives on the host you can just run:
1 | EXEC xp_fixeddrives; |
Conclusion
This is a pretty simple query to get SQL Server storage info.
If you’ve found this useful you might also like this script to get database size info