Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.04 KB | None | 0 0
  1. DECLARE @email_profile NVARCHAR(256) = 'kreirani operator na sql bazi'
  2. DECLARE @emailrecipients NVARCHAR(500) = 'mail'
  3. declare @clientName nvarchar(64) = 'projekt'
  4. declare @deltaMinutes int = 120
  5. DECLARE @subject NVARCHAR(MAX) = '['+@clientName+'] SQL Server Agent Job Failure Report: ' + @@SERVERNAME
  6.  
  7. --Dump report data to a temp table to be put into XML formatted HTML table to email out
  8. SELECT sjh.[server]
  9.     ,sj.NAME
  10.     ,sjh.step_id
  11.        ,sjh.step_name
  12.     ,sjh.[message]
  13.     ,sjh.run_date
  14.     ,sjh.run_time
  15.        ,msdb.dbo.agent_datetime(run_date, run_time) as 'RunDateTime'
  16.     ,   STUFF(STUFF(STUFF(RIGHT(REPLICATE('0', 8) + CAST(sjh.run_duration as varchar(8)), 8), 3, 0, ':'), 6, 0, ':'), 9, 0, ':') as run_duration
  17. into #TempJobFailRpt
  18. FROM msdb..sysjobhistory sjh
  19. INNER JOIN msdb..sysjobs sj ON (sj.job_id = sjh.job_id)
  20. WHERE 1=1
  21.     and msdb.dbo.agent_datetime(run_date, run_time) > dateadd(minute, -1*@deltaMinutes, getdate())
  22.     AND run_status != 4 -- Do not show status of 4 meaning in progress steps
  23.     AND run_status != 1 -- Do not show status of 1 meaning success
  24.     and sjh.step_id<>0
  25. ORDER BY sjh.run_time desc, sjh.step_id
  26.  
  27.  
  28.  
  29.  
  30. IF EXISTS (SELECT top 1 * FROM #TempJobFailRpt)
  31. BEGIN
  32.  
  33. -----Build report to HTML formatted email using FOR XML PATH
  34. DECLARE @tableHTML NVARCHAR(MAX) = '
  35. <html>
  36. <head>
  37. <style>
  38. </style>
  39. </head>
  40. <body>
  41. <table border="1" style="border-collapse: collapse">
  42. <tr>
  43.    <th style="width:10%; border: 1px solid #dddddd; text-align: left; padding: 8px;  text-decoration: underline">SQL Instance</th>
  44.    <th style="border: 1px solid #dddddd; text-align: left; padding: 8px; text-decoration: underline">Job Name</th>
  45.    <th style="border: 1px solid #dddddd; text-align: center; padding: 8px; text-decoration: underline">Step</th>
  46.    <th style="border: 1px solid #dddddd; text-align: justify; padding: 8px; text-decoration: underline">Error Details</th>
  47.    <th style="width:15%; border: 1px solid #dddddd; text-align: left; padding: 8px; text-decoration: underline">Job Run Date</th>
  48.    <th style="border: 1px solid #dddddd; text-align: left; padding: 8px; text-decoration: underline">Job Run Duration (DD:HH:MM:SS)</th>
  49. </tr>' + CAST((
  50.             SELECT td = [server]
  51.                 ,''
  52.                 ,td = NAME
  53.                 ,''
  54.                 ,td = step_id
  55.                 ,''
  56.                 ,td = [message]
  57.                 ,''
  58.                 ,td = RunDateTime
  59.  
  60.                 ,''
  61.                 ,td = run_duration
  62.  
  63.  
  64.             FROM #TempJobFailRpt a
  65.             ORDER BY run_time desc, step_id
  66.  
  67.  
  68.             FOR XML PATH('tr')
  69.                 ,TYPE
  70.                 ,ELEMENTS XSINIL
  71.             ) AS NVARCHAR(MAX)) + '
  72.    </table>
  73. </body>
  74. </html>';
  75.  
  76. EXEC msdb.dbo.sp_send_dbmail @profile_name = @email_profile
  77.     ,@recipients = @emailrecipients
  78.     ,@subject = @subject
  79.     ,@body = @tableHTML
  80.    ,@body_format = 'HTML'
  81.  
  82. --Drop Temp table
  83.     DROP TABLE #TempJobFailRpt
  84. END
  85. ELSE
  86. BEGIN
  87.     PRINT '*** No Records Generated ***'
  88.     DROP TABLE #TempJobFailRpt
  89. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement