SQL Server : 配置Mail发送邮件,并且让Job的邮件提示更详尽
配置邮件服务
1. Management 目录里面右击 Database Mail
2. 第一个配置选择第一项
以qq邮箱为例
确认后保存.然后点击下一步,直到完成.
发送测试邮件
收到邮件以后可以进行Job的设置
JOB邮件配置
方法一:
新增Operator
配置好确认,
然后打开SQL Server Agent的属性面板
启用Mail 配置
新建一个Job,在通知里面点击电子邮件,选择刚才创建的操作员.DataCenter, 保存. 至此方法一配置结束.
方法二
虽然方法一配置简单,但是每个job都要配置就很麻烦,而且邮件信息量很少. 下面是更改Job存储来达到自动发送邮件的功能,而且可以定义邮件内容.
修改系统存储 msdb.[dbo].[sp_sqlagent_log_jobhistory] ,在尾部加入SQL代码,调用邮件发送存储
IF ( @step_id > 0 AND @run_status NOT IN ( 1, 4 ) ) BEGIN EXEC msdb.dbo.usp_send_error_mail @job_id, @step_id, @sql_message_id, @sql_severity, @message, @run_status, @run_date, @run_time, @run_duration, @server, @retries_attempted END
发送mail存储:
CREATE PROCEDURE [dbo].[usp_send_error_mail] ( @job_id UNIQUEIDENTIFIER , @step_id INT , @sql_message_id INT = 0 , @sql_severity INT = 0 , @message NVARCHAR(4000) = NULL , @run_status INT , -- SQLAGENT_EXEC_X code @run_date INT , @run_time INT , @run_duration INT , @server sysname = NULL , @retries_attempted INT ) AS BEGIN DECLARE @jobname sysname -- 作业名称 DECLARE @profile_name NVARCHAR(128) --邮件配置文件 DECLARE @mail_address VARCHAR(MAX) -- 邮件接收地址 DECLARE @body NVARCHAR(MAX) -- 邮件正文 DECLARE @subject NVARCHAR(255) -- 邮件标题 SELECT @jobname = name FROM msdb.dbo.sysjobs WITH ( NOLOCK ) WHERE job_id = @job_id -- 设置DBA邮箱 SET @mail_address = ‘[email protected]‘ -- 设置数据库邮件配置文件 SET @profile_name = ‘sql server mail‘ -- 重试次数判断,加上短信报警 IF ( @retries_attempted >= 10 AND @retries_attempted % 10 = 0 ) BEGIN -- 2个小时内重试了10次则报警 IF ( ( SELECT DATEDIFF(mi, CAST(STUFF(STUFF(CAST(run_date AS NCHAR(8)), 7, 0, ‘-‘), 5, 0, ‘-‘) + N‘ ‘ + STUFF(STUFF(SUBSTRING(CAST(1000000 + run_time AS NCHAR(7)), 2, 6), 5, 0, ‘:‘), 3, 0, ‘:‘) AS DATETIME), GETDATE()) FROM msdb.dbo.sysjobhistory WITH ( NOLOCK ) WHERE job_id = @job_id AND retries_attempted = @retries_attempted - 10 ) < 120 ) BEGIN SET @mail_address = @mail_address + ‘[email protected]‘ END END -- 以下根据作业名个性化设置报错邮件接收地址 IF ( @job_id = ‘B7B792E5-DDAC-4954-B1F4-24163576A628‘ -- PRC_BranchCost ) SET @mail_address = @mail_address + ‘;[email protected]‘ -- 以上根据作业名个性化设置报错邮件接收地址 -- 设置邮件正文 SET @body = ‘服务器:‘ + ISNULL(CAST(@server AS NVARCHAR(255)), ‘‘) + CHAR(10) + ‘作业名:‘ + ISNULL(CAST(@jobname AS NVARCHAR(255)), ‘‘) + CHAR(10) + ‘步骤:‘ + CAST(@step_id AS NVARCHAR(10)) + CHAR(10) + ‘运行时间:‘ + STUFF(STUFF(CAST(@run_date AS NCHAR(8)), 7, 0, ‘-‘), 5, 0, ‘-‘) + N‘ ‘ + STUFF(STUFF(SUBSTRING(CAST(1000000 + @run_time AS NCHAR(7)), 2, 6), 5, 0, ‘:‘), 3, 0, ‘:‘) + CHAR(10) + ‘运行耗时:‘ + CASE WHEN @run_duration / 10000 > 23 THEN CAST(@run_duration / 10000 / 24 AS NVARCHAR(4)) + ‘day‘ + CAST(( @run_duration / 10000 ) % 24 AS NVARCHAR(2)) + ‘h‘ WHEN @run_duration / 10000 = 0 THEN ‘‘ ELSE CAST(( @run_duration / 10000 ) AS NVARCHAR(2)) + ‘h‘ END + CASE WHEN ( @run_duration % 10000 ) / 100 = 0 THEN ‘‘ ELSE CAST(( @run_duration % 10000 ) / 100 AS NVARCHAR(2)) + ‘m‘ END + CAST(@run_duration % 100 AS NVARCHAR(2)) + ‘s‘ + CHAR(10) + ‘重试次数:‘ + CAST(@retries_attempted AS NVARCHAR(10)) + CHAR(10) + ‘严重性:‘ + CAST(@sql_severity AS NVARCHAR(10)) + CHAR(10) + ‘消息ID:‘ + CAST(@sql_message_id AS NVARCHAR(10)) + CHAR(10) + ‘消息:‘ + ISNULL(@message, ‘‘) -- 设置邮件标题 SET @subject = ‘SQL作业错误:"‘ + ISNULL(CAST(@jobname AS NVARCHAR(255)), ‘‘) + ‘"在\\‘ + ISNULL(CAST(@server AS NVARCHAR(255)), ‘‘) EXEC msdb.dbo.sp_send_dbmail @profile_name = @profile_name, @recipients = @mail_address, @subject = @subject, @body = @body END
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。