ALTER PROCEDURE [dbo].[LogErrorEmail]
AS
declare @emailcontent nvarchar(2000);--邮件内容
declare @emailsubject nvarchar(200);--邮件主题
declare @emailadress nvarchar(100);--邮件发送地址
declare @error int; --错误数
declare @logfield nvarchar(max); --日志字段内容
declare @num int; --数据行数
set @error=0;
set @emailcontent='日期:'+convert(varchar(10),getdate(),120)+'<br>';
--tt1_log表检查
select @num=COUNT(*) from tt1_log where DATEDIFF(DD,t1_log.LOGDATE,getdate())<2
if(@num=2)
begin
select @logfield=LOG_FIELD from t1_log where DATEDIFF(DD,t1_log.LOGDATE,getdate())=0 --今天的数据
if(@logfield like '%error%')
begin
set @emailcontent=@emailcontent+'t1_log今天有错;' ;
set @error=@error+1;
end
select @logfield=LOG_FIELD from t1_log where DATEDIFF(DD,t1_log.LOGDATE,getdate())=1 --昨天的数据
if(@logfield like '%error%')
begin
set @emailcontent=@emailcontent+'t1_log昨天有错;' ;
set @error=@error+1;
end
end
else
begin
set @emailcontent=@emailcontent+'t1_log表数据不足两条;';
set @error=@error+1;
end
--t2_log表检查
select @num= COUNT(*) from t2_log where DATEDIFF(DD,t2_log.LOGDATE,getdate())<2
if(@num=2)
begin
select @logfield=LOG_FIELD from t2_log where DATEDIFF(DD,t2_log.LOGDATE,getdate())=0 --今天的数据
if(@logfield like '%error%')
begin
set @emailcontent=@emailcontent+'t2_log今天有错;' ;
set @error=@error+1;
end
select @logfield=LOG_FIELD from t2_log where DATEDIFF(DD,t2_log.LOGDATE,getdate())=1 --昨天的数据
if(@logfield like '%error%')
begin
set @emailcontent=@emailcontent+'t2_log昨天有错;' ;
set @error=@error+1;
end
end
else
begin
set @emailcontent=@emailcontent+'t2_log表数据不足两条;';
set @error=@error+1;
end
if(@error>0)
begin
exec msdb.dbo.sp_send_dbmail
@profile_name ='LogErrorEmail',--配置文件名称
@recipients='56789023@qq.com',--收件email地址
@subject=@emailsubject,--邮件主题
@body=@emailcontent, --邮件正文内容
@body_format='html' --邮件内容格式
end