zoukankan      html  css  js  c++  java
  • sqlserver2008数据库邮件

    读取job日志文章

    sqlserver2008数据库邮件:通过数据库邮件的方式,把job运行的日志发送到管理员的邮箱中

    --1.开启数据库高级属性
    exec sp_configure 'show advanced options',1
    reconfigure
    go
    exec sp_configure 'database mail xps',1
    reconfigure
    go

    --2.创建邮件帐户信息
    if exists(select name from msdb..sysmail_account where name =N'ETLErrorMailLog')
    begin
    exec msdb..sysmail_delete_account_sp @account_name='ETLErrorMailLog'
    end
    go
    EXEC msdb..sysmail_add_account_sp
    @ACCOUNT_NAME = 'ETLErrorMailLog', -- 邮件帐户名称
    @EMAIL_ADDRESS = 'emmy_lee@126.com', -- 发件人邮件地址
    @DISPLAY_NAME = '系统管理员', -- 发件人姓名
    @REPLYTO_ADDRESS = NULL,
    @DESCRIPTION = NULL,
    @MAILSERVER_NAME = 'SMTP.126.COM', -- 邮件服务器地址
    @MAILSERVER_TYPE = 'SMTP', -- 邮件协议
    @PORT = 25, -- 邮件服务器端口
    @USERNAME = 'emmy_lee@126.com', -- 用户名
    @PASSWORD = 'XXXXXXXXXXX', -- 密码
    @USE_DEFAULT_CREDENTIALS = 0,
    @ENABLE_SSL = 0,
    @ACCOUNT_ID = NULL
    GO
    --3.数据库配置文件
    if exists(select name from msdb..sysmail_profile where name=N'ETLErrorProfileLog')
    begin
    exec msdb..sysmail_delete_profile_sp @profile_name='ETLErrorProfileLog'
    end
    exec msdb..sysmail_add_profile_sp @profile_name = 'ETLErrorProfileLog', -- profile 名称
    @description = '数据库邮件配置文件', -- profile 描述
    @profile_id = null
    go
    --4.用户和邮件配置文件相关联
    exec msdb..sysmail_add_profileaccount_sp @profile_name = 'ETLErrorProfileLog', -- profile 名称
    @account_name = 'ETLErrorMailLog', -- account 名称
    @sequence_number = 1 -- account 在 profile 中顺序
    go

    --5.发送文本测试邮件
    exec msdb..sp_send_dbmail @profile_name='ETLErrorProfileLog'
    ,@recipients='lxwcel@126.com' --收件人
    ,@subject='aa'
    ,@body='aa'
    go

    --6.发送job日志邮件
    declare @sqlQuery nvarchar(max)
    set @sqlQuery=
    N'<H1 align="center">Job日志列表</H1>' +
    N'<table border="1" cellspacing="0" cellPadding="5" style="line-height:25px; font-size:12px;">' +
    N'<tr style="background:#e1e1e1;"><th nowrap>作业名称</th><th nowrap>步骤名称</th>' +
    N'<th nowrap>运行时间</th><th nowrap>持续时间</th><th nowrap>状态</th><th nowrap>日志详情</th>' +
    N'</tr>' +
    CAST((SELECT td=jobs.name,'',
    td=history.step_name,'',
    td=(left(left(run_date,4)+'-'+right(run_date,4),7)+'-'+right(run_date,2)),'',
    td=(left(left(left('000000',6-len(run_duration))+ltrim(run_duration),2)+':'+right(left('000000',6-len(run_duration))+ltrim(run_duration),4),5)+':'+right(left('000000',6-len(run_duration))+ltrim(run_duration),2)) ,'',
    td=(case run_status when 0 then '失败' when 1 then '成功' when 2 then '重试' when 3 then '已取消' when 4 then '正在进行中' end),'',
    td=history.message ,''
    FROM msdb.dbo.sysjobhistory history INNER JOIN msdb.dbo.sysjobs jobs ON history.job_id=jobs.job_id
    WHERE jobs.name = 'myjob' AND step_id <> 0 AND run_date = cast(replace(convert(date,getdate()),'-','') as int)
    for xml path('tr'),type) as nvarchar(max))+
    N'</table>' ;
    exec msdb..sp_send_dbmail @profile_name=ETLErrorProfileLog,
    @recipients='lxwcel@126.com',--收件人
    @subject='job执行日志',
    @body=@sqlQuery,
    @body_format='html'
    go


    读取ssis日志

    ------------ sysssislog日志
    SELECT source,starttime,endtime,message
    FROM msdb.dbo.sysssislog
    WHERE CONVERT(DATE,starttime) = CONVERT(DATE,GETDATE()-1)
    AND event = 'OnError' AND datacode = '-1071607780'
    go

    --发送SSIS中的日志
    DECLARE @TableSSISHTML NVARCHAR(MAX); --存放日志列表
    SET @TableSSISHTML =
    N'<H1 align="center">SSIS日志列表</H1>' +
    N'<table border="1" cellspacing="0" cellPadding="5" style="line-height:25px; font-size:13px;">' +
    N'<tr style="background:#e1e1e1;"><th nowrap>包文件</th>' +
    N'<th nowrap>作业开始时间</th><th nowrap>作业结束日期</th><th nowrap>日志详情</th>' +
    N'</tr>' +
    CAST ( ( SELECT td = source, '',
    --td = history.server, '',
    td = CONVERT(VARCHAR(8),starttime,108), '',
    td = CONVERT(VARCHAR(8),endtime,108), '',
    td = message, ''
    FROM msdb.dbo.sysssislog
    WHERE CONVERT(DATE,starttime) = CONVERT(DATE,GETDATE()-1)
    AND event = 'OnError' AND datacode = '-1071607780'
    FOR XML PATH('tr'), TYPE
    ) AS NVARCHAR(MAX) ) +
    N'</table>' ;
    EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'ETLErrorProfileLog',
    @recipients = 'lxwcel@126.com',
    @subject = 'SSIS日志列表',
    @body = @TableSSISHTML,
    @body_format = 'HTML'



     

  • 相关阅读:
    细思极恐-你真的会写java吗?
    java定时器无法自动注入的问题解析(原来Spring定时器可以这样注入service)
    java并发编程之volatile
    细思极恐-你真的会写java吗?
    细思极恐-你真的会写java吗?
    细思极恐-你真的会写java吗
    1月中旬值得一读的10本技术新书(机器学习、Java、大数据等)!
    0基础小白怎么学好Java?
    java日常知识点积累
    (解决)ECSHOP info: Can't Connect MySQL Server(localhost:3306)!转删
  • 原文地址:https://www.cnblogs.com/emmy/p/2239195.html
Copyright © 2011-2022 走看看