此脚本需要启用DBMail以支持邮件的发送。可设为每日运行的JOB以追踪自增长事件,以便做出适当的调整。
-- Email the Auto-growth events that have occurred in the last 24 hours to the DBA-- This script will email DBA if a auto-grow event occurred in the last day-- Date: 3/09/2026DECLARE @filename NVARCHAR(1000);DECLARE @bc INT;DECLARE @ec INT;DECLARE @bfn VARCHAR(1000);DECLARE @efn VARCHAR(10);DECLARE @DL VARCHAR(1000); -- email distribution listDECLARE @ReportHTML NVARCHAR(MAX);DECLARE @Subject NVARCHAR (250);-- Set email distrubution list valueSET @DL = 'xxxx@qq.com' -- Chanage these to the recipients you wish to get the email-- Get the name of the current default traceSELECT @filename = CAST(value AS NVARCHAR(1000))FROM ::fn_trace_getinfo(DEFAULT)WHERE traceid = 1 AND property = 2;-- SELECT @filename-- rip apart file name into piecesSET @filename = REVERSE(@filename);SET @bc = CHARINDEX('.',@filename);SET @ec = CHARINDEX('_',@filename)+1;SET @efn = REVERSE(SUBSTRING(@filename,1,@bc));SET @bfn = REVERSE(SUBSTRING(@filename,@ec,LEN(@filename)));-- set filename without rollover numberSET @filename = @bfn + @efnSELECT @filename-- Any Events Occur in the last dayIF EXISTS (SELECT * FROM ::fn_trace_gettable(@filename, DEFAULT) AS ftg WHERE (EventClass = 92 -- Date File Auto-grow OR EventClass = 93) -- Log File Auto-grow AND StartTime > DATEADD(dy,-1,GETDATE())) BEGIN -- If there are autogrows in the last day SET @ReportHTML = N'' + N'Auto-grow Events for ' + CAST(SERVERPROPERTY('MachineName') AS NVARCHAR(128)) + + CASE WHEN SERVERPROPERTY('InstanceName') IS NULL THEN '' ELSE N'\' + CAST(SERVERPROPERTY('InstanceName') AS NVARCHAR(128)) END + N'
' + N'
Start Time | Event Name | ' + N'Database Name | File Name | Growth in MB | ' + N'Duration in MS |
---|
效果图如下: