博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件发送过去24小时的自增长事件(MSSQL)
阅读量:6079 次
发布时间:2019-06-20

本文共 3400 字,大约阅读时间需要 11 分钟。

此脚本需要启用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'
' + N'
' + N'
' + N'
' + CAST((SELECT td = ftg.StartTime, '', td = te.name, '', td = DB_NAME(ftg.DatabaseID), '', td = [FileName], '', td =(ftg.IntegerData*8)/1024.0, '', td = (ftg.Duration/1000) FROM ::fn_trace_gettable(@filename, DEFAULT) AS ftg INNER JOIN sys.trace_events AS te ON ftg.EventClass = te.trace_event_id WHERE (EventClass = 92 -- Date File Auto-grow OR EventClass = 93) -- Log File Auto-grow AND StartTime > DATEADD(dy,-1,GETDATE()) -- Less than 1 day ago ORDER BY StartTime FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) + N'
Start Time Event Name Database Name File Name Growth in MB Duration in MS
' ; -- Build the subject line with server and instance name SET @Subject = 'Auto-grow Events in Last Day ' + CAST(SERVERPROPERTY('MachineName') AS NVARCHAR(128)) + + CASE WHEN SERVERPROPERTY('InstanceName') IS NULL THEN '' ELSE N'\' + CAST(SERVERPROPERTY('InstanceName') AS NVARCHAR(128)) END -- Send email to distribution list. EXEC msdb.dbo.sp_send_dbmail @recipients=@DL, @subject = @Subject, @body = @ReportHTML, @body_format = 'HTML' -- @profile_name='webmaster' ; -- Change this to your profile nameEND; -- If there are autogrows in the last day

 效果图如下:

转载于:https://www.cnblogs.com/Geton/p/5256743.html

你可能感兴趣的文章
从头认识java-13.11 对照数组与泛型容器,观察类型擦除给泛型容器带来什么问题?...
查看>>
Python-MacOSX下SIP引起的pip权限问题解决方案(非取消SIP机制)
查看>>
从MFQ方法到需求分析
查看>>
android.view.WindowManager$BadTokenException: Unable to add window
查看>>
HDU5012:Dice(bfs模板)
查看>>
iphone openssh
查看>>
Linux下MEncoder的编译
查看>>
Javascript中闭包(Closure)的探索(一)-基本概念
查看>>
spark高级排序彻底解秘
查看>>
ylbtech-LanguageSamples-PartialTypes(部分类型)
查看>>
福建省促进大数据发展:变分散式管理为统筹集中式管理
查看>>
开发环境、生产环境、测试环境的基本理解和区别
查看>>
tomcat多应用之间如何共享jar
查看>>
Flex前后台交互,service层调用后台服务的简单封装
查看>>
MySQL入门12-数据类型
查看>>
Windows Azure 保留已存在的虚拟网络外网IP(云服务)
查看>>
修改字符集
查看>>
HackTheGame 攻略 - 第四关
查看>>
js删除数组元素
查看>>
带空格文件名的处理(find xargs grep ..etc)
查看>>