Problem
When looking for impending problems due to lack of disk space it’s necessary to know how much space is available on each drive. There have been other tips written about how to do this, but in this tip I show you a way this can be done using SQLCLR.
Solution
The .Net framework provides this information very readily through the DriveInfo object, which can be invoked and the information extracted and returned using a SQLCLR Tabled Valued Function. Table Valued Functions (TVFs) are SQLCLR routines that return multiple rows of multi-column output. This article shows how they are implemented.
Before going too far it should be noted that there are other solutions to the problem of finding the amount of free space on each drive. In particular, if you’re willing to use undocumented system external stored procedures you can use the sys.xp_fixeddrives. Use of that procedure is used in the article Determine Free Disk Space in SQL Server with T-SQL Code. Of course, xp_fixeddrives is going to query Windows to get the information it returns.
The .Net framework exposes the functionality of the Windows operating system and the System.IO.DriveInfo class is the one responsible for information on drives of all types. It’s static method GetDrives returns an array of DriveInfo objects and each DriveInfo object has the details on one of the drives. GetDrives returns information on all drives that are connected to the system whether they’re fixed disks, network drives, full or empty CDRoms.
Most systems will have multiple drives and there are several items of information available for each drive. This combines to make the TVF an ideal mechanism for exposing drive information. Each drive will be represented by a row. Each row has several columns of information such as the drive letter, total megabytes, free megabytes, volume label, etc.
To create a TVF, create a new Visual Studio 2008 project. Choose either a C# SQL Server Project or a VB.Net SQL Server Project in the Database Project\SQL CLR section. It’s also possible to code SQLCLR routines in manged C++, but there isn’t a built-in project type for C++ so you’ll have to code and deploy without the aid of a template. This example creates a C# CLR function.
Next Visual Studio asks for a Database Reference. Pick a reference to the database where you want to create the function or Add a New Reference for a database that doesn’t yet have a reference. Here’s the dialog that is shown as I select a reference to the ns_lib database on my laptop:
Visual Studio creates a project with only a dummy test script called Test.sql. Add the file for the function by right clicking on the project in Solution Explorer and selecting Add -> User-Defined Function. Alternatively you can use the Visual Studio Project Menu and choose Add User-Defined Function. You’ll see this dialog:
Build and Deploy the function with Visual Studio’s Build -> Deploy menu.
Finally, to use drive_info, invoke it from SSMS:
select * from dbo.drive_info()
On my laptop the results are:
letter total_mb free_mb format type volume_label
—— ——— ——– ——- ———- ————
C 244095 75851 NTFS Fixed
D NULL NULL NULL CDRom NULL
E 95393 59159 NTFS Fixed 100GMobile
H 76319 76227 NTFS Removable New Volume
(4 row(s) affected)
Next Steps
- Build the drive_info TVF on your system.
- Incorporate the information into your drive space early warning system such as the one described in Capacity Planning for SQL Server 2000 Database Storage.
- Consider using a TVF instead of a stored procedure when you must return system information.
- Download the C# code here

Andy Novick is a SQL Server Developer in the Boston area with 25 years of database and application development experience. His consulting practice focuses on building applications, including software products that use SQL Server’s capabilities to the maximum. He has developed both OLTP and data warehouse databases with multi-terabytes scale. He has particular expertise in automating data management for such large databases. Other recent projects have included ETL, Security and SQL Injection prevention. Andy is the originator of the popular “SQL Server Loadfest” event held in Waltham, MA.
- MSSQLTips Awards: Trendsetter (25+ tips) – 2014


