sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Monitor Disk Space for SQL Server databases

Antonios Chatzipavlis
Thursday 16 August 2018





Job Code

declare @threshold_space_pct decimal(5,2) = 99.000;
declare @ebody nvarchar(max) ='';
with r as (
select distinct     v.volume_mount_point as drive
                ,   v.total_bytes as drive_size_in_bytes
                ,   v.available_bytes as drive_free_space_in_bytes
                ,   p.drive_free_space_pct
                ,   iif (p.drive_free_space_pct<=@threshold_space_pct,1,0) as low_space
from sys.master_files AS f  
cross apply sys.dm_os_volume_stats(f.database_id, f.file_id) as v
cross apply ( values (cast ( (v.available_bytes * 1.0 ) / (v.total_bytes * 1.0 ) as  decimal(5,2) )  * 100.0 ) ) as p(drive_free_space_pct)
)
select @ebody += drive + ' detected with low space ' + CHAR(10)+CHAR(13) from r where low_space = 1 ; 
if ( len(@ebody) > 0 )
begin
    declare @subject nvarchar(1024) = 'Low disk space detected in SQL Server instance ' +  @@Servername  
    EXEC msdb.dbo.sp_send_dbmail 
                @profile_name = 'YourProfile',
                @recipients = 'dba@sqlschool.gr',
                @body = @ebody,
                @subject = @subject
end


//antonch


Comments

05 Oct 2018 @ 7:42 PM

user-gravatar

ΓΙΑΝΝΑΡΟΣ ΔΗΜΗΤΡΗΣ

Είναι μια θαυμάσια προσπάθεια αυτά τα καινούργια Vlogs.Πολύ βολικό και και για DBA's αλλά σωτήριο για τους "accidental DBA's"

08 Oct 2018 @ 1:57 PM

user-gravatar

Vassilis

Πολύ καλό και εύκολο vlog.Να πώω μόνο ότι για να μου παίξει έπρεπε να αλλάξω το παρακάτω: -- , iif (p.drive_free_space_pct<=@threshold_space_pct,1,0) as low_spaceμε το παρακάτω: ,case when p.drive_free_space_pct<=@threshold_space_pct then 1 else 0 end as low_space Ευχαριστώ και πάλι.

08 Oct 2018 @ 2:35 PM

user-gravatar

Antonios Chatzipavlis

@Vassilis Προφανώς είσαι σε έκδοση του SQL Server πριν το 2012 όπου δεν υπάρχει η IIF. Γιατί?

08 Oct 2018 @ 3:04 PM

user-gravatar

Vassilis

@Antonios Γιατί : "αφού δουλεύει γιατί να το αλλάξω" Με αποδείξεις σε lab για το πόσο ποιο γρήγορα θα έτρεχε μία νέα version και πόσο ποιο εύχρηστο θα γίνονταν το Reporting με τα SSRS . Ευτυχώς που σταματάει η υποστήριξη του χρόνου και βγήκαν και τα "ψηφιακά άλματα" και οι "ποιοτικοί εκσυγχρονισμοί" μπας και τρέξουμε κανένα σύγχρονο query. Ειδικά τα data-driven subscriptions τα περιμένω και ξερογλείφομαι.

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn

sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.