I created a SP to monitor SQL Server Agent Jobs and generating Email for the same in HTML Format.
However, I want to conditionally format the Run Status column to be Red/Green based on cell value(TEXT-Success/Fail).
Please find the code at https://social.msdn.microsoft.com/Forums/sqlserver/en-US/2663e23b-0b55-4816-beb8-fdf8a020396d/formating-msdbdbospsenddbmail?forum=transactsql
(Marked Answer by Hilary Cotter(MCC, MVP))
Kindly help me with the missing code to implement this feature.
Thank You.
Here's how you can modify the creation of the HTML to work:
DECLARE #tableHTML nvarchar(max);
SET #tableHTML =
N'<style type="text/css">h2, body {font-family: Arial, verdana;}
table{font-size:11px; border-collapse:collapse;}
td{background-color:#F1F1F1; border:1px solid black; padding:3px;}
th{background-color:#99CCFF;}</style>'
+ N'<h2>Recent Job Status</h2>'
+ N'<table border="1">' + N'<tr>
<th>Job Name</th>
<th>Last Run Date</th>
<th>Last Run Time</th>
<th>Last Run Status</th>'
+ CAST(( SELECT td = j.[name],
'',
td = MAX(jh.run_date),
'',
td = MAX(run_time),
'',
td = CASE run_status
WHEN 0
THEN N'<font color="red">Failure</font>'
WHEN 1
THEN N'<font color="green">Success</font>'
WHEN 2
THEN N'<font color="yellow">Retry</font>'
WHEN 3
THEN N'<font color="blue">Cancellation</font>'
WHEN 4
THEN N'<font color="black">In Progress</font>'
ELSE N'<font color="black">Unknown</font'
END
FROM dbo.sysjobhistory AS jh
INNER JOIN dbo.sysjobs AS j
ON j.job_id = jh.job_id
WHERE CONVERT(datetime,
SUBSTRING(CONVERT(varchar(12), jh.run_date),
0, 5) + N'-'
+ SUBSTRING(CONVERT(varchar(12), jh.run_date), 5, 2) + '-'
+ SUBSTRING(CONVERT(varchar(12), jh.run_date), 7, 2)) > DATEADD(day, -1, GETDATE())
AND j.category_id <> 14
GROUP BY j.[name], jh.run_status
FOR XML PATH('tr'), TYPE) AS NVARCHAR(MAX)) + N'</table>';
SET #tableHTML = REPLACE(REPLACE(#tableHTML, N'<', N'<'), N'>', N'>');
For my system, that then outputs this:
Hope that's what you need.
Related
I need to generate automatic email alert, in which
- the query should be in MySQL(ready)
- how to embed it in HTML and make connections (no idea)
I did the same thing with SQL server I don't have any idea about MySql.
Here is the code for SQL which I did.
declare #tableHTML NVARCHAR(MAX),#s1 varchar (max), #s2 varchar (max) ,
#s3 varchar (max);
set #s1 = 'Sales Data Of All Stores Of CN and HK '
set #s2 = (convert(varchar, getdate()-1, 107))
set #tableHTML = '<H1>Sales Data Of All Stores Of CN and HK </H1>
<table border="1" cellpadding="10">
<style>
table,th,td
{
border="5" bordercolor="#ff0000";
}
table
{
BORDER=10 BORDERCOLOR="#0000FF" BORDERCOLORLIGHT="#33CCFF"
BORDERCOLORDARK="#0000CC"
}
td
{
height:40px;
}
tr
{
background-color:green;
color:white;
}
th
{
background-color:yellow;
color:black;
}
</style>
<tr>
<th>S.NO.</th>
<th>STORE NO.</th>
<th>BUSINESS DATE</th>
<th>TOTAL NO. OF TRANSACTIONS </th>
<th>TOTAL NO. OF RETURNS </th>
<th>TOTAL NO. OF BILL CANCELLATIONS</th>
<th>TOTAL AMOUNT</th>
</tr>' +
CAST ( (
select *
FROM XYZ
WHERE X=Y
GROUP BY X
FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) )+'</table>';
--Print #tableHTML;
set #s3 = #s1 + #s2
exec msdb.dbo.sp_send_dbmail
#recipients=''
, #profile_name=''
, #subject= #s3
, #body = #tableHTML
, #body_format = 'HTML'
I need the same result for Mysql using SQLYog.Please Help me out with
this.
I am using SQL+HTML script to send an email containing top 5 columns of a table mytable. Currently, I have all the column titles aligned centrally and each column entries aligned to the right using [th] and [td] repsectively. How can I make entries in Vendor column left aligned while keeping everything else the same?
DECLARE #dt datetime, #prev_dt datetime
SELECT #dt = dbo.GetDateOnly(getdate())
SELECT #prev_dt = dbo.GetDateOnly(getdate()-1)
SELECT top 5
Vendor,
OUTL_New_$,
OUTL_Old_$,
Outl_Diff_$ ,
OUTL_Diff_Perc,
Analyst
into #vpositive
FROM mytable
ORDER BY 4 DESC
--select * from #vpositive
if(##ROWCOUNT>0)
begin
--drop table #email
DECLARE #Body2 varchar(max)
declare #TableHead2 varchar(max)
declare #TableTail varchar(max)
Set #TableTail = '</table></body></html>';
Set #TableHead2 = '<html><head>' +
'<hr style="height:1px;border:none;color:#333;background-color:#333;">'+
'<H5 style="color: #000000; font-family:Arial">2: VENDOR LEVEL TOP 5 OUTL $ <font color="blue">INCREMENTS -</font></H5>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:10pt;color:Black;text-align:right;font-family:Arial;} ' +
'th {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:10pt;color:Black;text-align:center;font-family:Arial;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0 width=auto>' +
'<tr bgcolor=#007336>'+
'<th><b><font color="white">Vendor</font></b></th>' +
'<th><b><font color="white">OUTL$ New</font></b></th>' +
'<th><b><font color="white">OUTL$ Old</font></b></th>' +
'<th><b><font color="white">OUTL$ Diff</font></b></th>' +
'<th><b><font color="white">OUTL$ Diff Perc</font></b></th>' +
'<th><b><font color="white">Analyst</font></b></th></tr>';
--Select information for the Report--
Select #Body2= (select
substring(Vendor,1,60) As [TD]
,'$ '+substring(parsename(convert(varchar,convert(money,cast(OUTL_New_$ as int)),1),2),1,13) As [TD]
,'$ '+substring(parsename(convert(varchar,convert(money,cast(OUTL_Old_$ as int)),1),2),1,13) As [TD]
,'$ '+substring(parsename(convert(varchar,convert(money,cast(OUTL_Diff_$ as int)),1),2),1,13) As [TD]
,substring(CAST(OUTL_Diff_Perc AS varchar(10)),1,4)+' %' As [TD]
,substring(Analyst,1,20) As [TD]
from #vpositive
For XML raw('tr'), Elements)
Set #Body = #TableHead2 + #Body2 + #TableTail
--Email
EXEC msdb.dbo.sp_send_dbmail
#recipients='abc#abc.com',
#subject = My analysis',
#body = #Body,
#body_format = 'HTML';
end
Did you try this:
<th align="left">
or this:
text-align: left;
I'm having a problem sending a HTML formatted email from SQL server.
With the following section of code I get a "Line 1, incorrect syntax near '<'" error.
SET #tableHTML =
'<H1>Progress Report</H1>' +
'<table border="1">' +
'<tr>' +
'<th>Project Name</th>' +
'<th>Platform</th>' +
'<th>Due By</th>' +
'<th>Current Status</th>' +
'<th>Current State</th>' +
'</tr>' +
CAST (
(
SELECT
td = [Project Name], ' ',
td = Platform, ' ',
td = [Due By], ' ',
td = [Current Status], ' ',
td = [Current State], ' '
FROM [dbo].[table_name]
ORDER BY [Current Status] DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
'</table>' ;
I cant seem to pin it to anything in particular? Any Idea's?
Thanks
Update 1:
Ok I've run the code in a debug session and have checked the contents of #tableHTML, the contents looks fine and it gets populated with the expected data from my Table.
Meaning the errors coming in from somewhere else, so I've copied the whole query this time.
DECLARE #tableHTML NVARCHAR(MAX);
SET #tableHTML =
'<h1>Progress Report</h1>' +
'<table border="1">' +
'<tr>' +
'<th>Project Name</th>' +
'<th>Platform</th>' +
'<th>Due By</th>' +
'<th>Current Status</th>' +
'<th>Current State</th>' +
'</tr>' +
CAST
(
(
SELECT
td = [Project Name], '',
td = Platform, '',
td = [Due By], '',
td = [Current Status], '',
td = [Current State], ''
FROM [dbo].[table_name]
ORDER BY [Current Status] DESC
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
'</table>';
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'db_mail_account',
#recipients = 'example#example.com',
#subject = 'Daily Project Tracking Report',
#query = #tableHTML,
#body_format = 'HTML';
Thanks again.
It looks like you want #tableHTML to be the body of the email, but you're passing it in as #query, which has to contain valid SQL, hence the error.
Try using #body instead:
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'db_mail_account',
#recipients = 'example#example.com',
#subject = 'Daily Project Tracking Report',
#body = #tableHTML,
#body_format = 'HTML';
I am using html code in my SQL Script to output results from a table in an email, but I need to change the color of the text if any of the results come back as specific string. Here is a snippet of what I'm using to produce the table:
'<table border="2">' +
'<tr>
<th>No#</th>
<th>SERVER NAME</th>
<th>SERVER IP</th>
<th>STATUS </th>
</tr>' +
CAST (( SELECT DISTINCT
td = [NUMBER], '',
td = [SERVER NAME], '',
td = [SERVER IP], '',
td = [STATUS], ''
FROM SUMMARY
FOR XML PATH('tr'), TYPE
) AS VARCHAR(MAX) ) +
'</table>';
So I am concerned about the "STATUS" Field, if it comes back SUCCESS it could stay as regular text, if it says BAD then I want it to be Red.... I have tried putting in the <font color=""> tag but it only changes the Headers (between the TH tags)..
No# SERVER NAME SERVER IP STATUS
1 SERVER1 10.10.10.1 BAD --this should be red
2 SERVER2 10.10.10.2 SUCCESS
3 SERVER3 10.10.10.3 BAD --this should be red
4 SERVER4 10.10.10.4 SUCCESS
Like the commenter noted, you should really not be doing this. If you really want to, you will need to do it outside the query, since SQL will treat all query data as text and escape the HTML characters in order to display them as text.
A simple (but "ugly-ish") solution is to simply do a replace like this:
REPLACE('<table border="2">' +
'<tr>
<th>No#</th>
<th>SERVER NAME</th>
<th>SERVER IP</th>
<th>STATUS </th>
</tr>' +
CAST (( SELECT DISTINCT
td = [NUMBER], '',
td = [SERVER NAME], '',
td = [SERVER IP], '',
td = [STATUS], ''
FROM SUMMARY
FOR XML PATH('tr'), TYPE
) AS VARCHAR(MAX) ) +
'</table>', '>BAD<', '><font color="red">BAD</font><')
I am trying to send 2 different tables inside of 1 email. I want one table to establish data concerning data stored in 1 database and the other table for a second database of information. I can produce 1 email simply enough but i cannot understand how to set a second table with a completely different query and data in the same email.
here is what i am currently working with:
USE MY DATABASE
DECLARE
#tableHTML NVARCHAR(MAX)
SET #tableHTML =
N'<H1>Lacrosse Inquiries</H1>' +
N'<table border="1" style="text-align: middle;" width:100% height:20px style="cell" cellspacing="0" cellpadding="5">' +
N'<tr><th>FIRST NAME</th><th>LAST NAME</th><th>Entry Term</th></th><th>Market Segment</th><th>State</th><th>Home Number</th><th>Cell Phone</th>'+
CAST ( ( SELECT td = INQUIRY.F_NAME , ''
, td = INQUIRY.L_NAME , ''
, td = INQUIRY.ENTRY_TERM , ''
, td = INQUIRY.MARKET_SEG , ''
, td = EWADDRESS.STATE , ''
, td = EWADDRESS.HOME_PHONE , ''
, td = EWADDRESS.WORK_PHONE , ''
from EWSTUDENT AS INQUIRY inner join EWMULTI1M as sport on sport.EWSTUID=INQUIRY.EWSTUID left JOIN EWADDRESS ON EWADDRESS.EWSTUID=INQUIRY.EWSTUID LEFT JOIN ESTUDENT ON ESTUDENT.EWSTUID=INQUIRY.EWSTUID
where VCODETYPE = 'EACTIV1M' AND VCODE ='CLAX' AND INQUIRY.GENDER = 'F' AND STUD_TYPE ='P' and (CONVERT(VARCHAR(8), sport.CREATE_DT, 112) = CONVERT(varchar(8), GETDATE(), 112))
order by INQUIRY.L_NAME
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
EXEC msdb.dbo.sp_send_dbmail
#profile_name= 'Jordan.Sorensen',
#recipients = 'Jordan.sorensen#svu.edu',
#from_address = 'Jordan.Sorensen#svu.edu',
#body = #tableHTML,
#execute_query_database= 'MY DATABASE',
#subject ='Womens Lacrosse Inquiries',
#body_format= 'HTML'
GO
ANY SUGGESTIONS ON HOW TO CREATE TWO DIFFERENT TABLES WITH TWO COMPLETELY DIFFERENT SETS OF DATA IN ONE EMAIL?
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' --<-- This is where you 1st query ends add a couple of line breaks
+ N'<br/><br/>' -- and add you second table just like you have done before
+ N'<table border="1" style="text-align: middle;" width:100% height:20px style="cell" cellspacing="0" cellpadding="5">'
+ N'<tr><th>FIRST NAME</th><th>LAST NAME</th><th>Entry Term</th></th><th>Market Segment</th><th>State</th><th>Home Number</th><th>Cell Phone</th>'
+ CAST ( (/*Your Select just like you have done for 1st table*/) AS NVARCHAR(MAX))
+ N'</table>'
Obviously HTML generated from both sql queries are concatenated and set the value to variable #tableHTML, the rest of the stuff remains the same passing parameters to your procedure and etc etc.