Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Email Formatted HTML Table with T-SQL


Email Formatted HTML Table with T-SQL

Author
Message
SQL-DBA
SQL-DBA
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 461
Comments posted to this topic are about the item Email Formatted HTML Table with T-SQL
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44948 Visits: 39860
I don't believe you'll find anyone more pro-xp_CmdShell or pro_BCP than myself but I can't figure out why you bothered with either for this task since sp_SendDBMail could have easily handled this task. And having to install a 3rd party piece of shareware on a server certainly doesn't make for a "T-SQL Only" project, IMHO.

Why didn't you just use sp_SendDBMail which can take a 2GB VARCHAR(MAX) body with the custom HTML formatting from your article?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
SQL-DBA
SQL-DBA
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 461
Jeff Moden (6/19/2013)
I don't believe you'll find anyone more pro-xp_CmdShell or pro_BCP than myself but I can't figure out why you bothered with either for this task since sp_SendDBMail could have easily handled this task. And having to install a 3rd party piece of shareware on a server certainly doesn't make for a "T-SQL Only" project, IMHO.

Why didn't you just use sp_SendDBMail which can take a 2GB VARCHAR(MAX) body with the custom HTML formatting from your article?



Thanks for the feedback Jeff!

Basically, I think you are right. I should have written the article with a more modern twist. The practical reason is that I wear many different hats so I end up using Blat for SysAdmin tasks, AD notifications, sending log files, etc. And I was using Blat with SQL Server 7.0. It is just an old habit that works so I haven't changed it.
I_Slater
I_Slater
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 29
Hey,

This is a Email Formatted HTML table i like to use,..
im sure it would be usefull and easy to use as it is for me :-)

-------------------------------------------------------------------------

-- Email Querry--
DECLARE @Body varchar(max)
declare @TableHead varchar(max)
declare @TableTail varchar(max)
declare @mailitem_id as int
declare   @statusMsg as varchar(max)
declare   @Error as varchar(max)
declare   @Note as varchar(max)

Set NoCount On;
set @mailitem_id = null
set @statusMsg = null
set @Error = null
set @Note = null
Set @TableTail = '</table></body></html>';

--HTML layout--
Set @TableHead = '<html><head>' +
    '<H1 style="color: #000000">HEADER OF TABLE</H1>' +
'<style>' +
'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:9pt;color:Black;} ' +
'</style>' +
'</head>' +
'<body><table cellpadding=0 cellspacing=0 border=0>' +
'<tr bgcolor=#F6AC5D>'+
'<td align=center><b>Name of column</b></td>' +
'<td align=center><b>Name of column</b></td>' +
'<td align=center><b>Name of column</b></td>' +
'<td align=center><b>Name of column</b></td>' +
'<td align=center><b>Name of column</b></td></tr>';

--Select information for the Report--
Select @Body= (Select
Column As [TD],
Column As [TD],
Column As [TD],
Column As [TD],
Column As [TD]

FROM [DB].[dbo].[Table]
where -condition-
(whatever you want to do else ...)

For XML raw('tr'), Elements)

-- Replace the entity codes and row numbers
Set @Body = Replace(@Body, '_x0020_', space(1))
Set @Body = Replace(@Body, '_x003D_', '=')
Set @Body = Replace(@Body, '<tr><TRRow>1</TRRow>', '<tr bgcolor=#C6CFFF>')
Set @Body = Replace(@Body, '<TRRow>0</TRRow>', '')


Set @Body = @TableHead + @Body + @TableTail

-- return output--
Select @Body

--Email
EXEC msdb.dbo.sp_send_dbmail
   @profile_name =email', <--This is the mail account to sent from.
   @mailitem_id = @mailitem_id out,
@recipients='blah@blah.co.za',
@subject = 'subject Email',
@body = @Body,
@body_format = 'HTML';
nakache
nakache
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 211
Why not using CSS?
Example:

   DECLARE @bodyMsg nvarchar(max)
   DECLARE @subject nvarchar(max)
   DECLARE @tableHTML nvarchar(max)

   SET @subject = 'Query Results in HTML with CSS'
   
   
         SET @tableHTML =
               N'<style type="text/css">
                  #box-table
                  {
                     font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
                     font-size: 12px;
                     text-align: center;
                     border-collapse: collapse;
                     border-top: 7px solid #9baff1;
                     border-bottom: 7px solid #9baff1;
                  }
                  #box-table th
                  {
                     font-size: 13px;
                     font-weight: normal;
                     background: #b9c9fe;
                     border-right: 2px solid #9baff1;
                     border-left: 2px solid #9baff1;
                     border-bottom: 2px solid #9baff1;
                     color: #039;
                  }
                  #box-table td
                  {
                     border-right: 1px solid #aabcfe;
                     border-left: 1px solid #aabcfe;
                     border-bottom: 1px solid #aabcfe;
                     color: #669;
                  }
                  tr:nth-child(odd)      { background-color:#eee; }
                  tr:nth-child(even)      { background-color:#fff; }                  
                  </style>'+   
               N'<H3><font color="Red">All Rows From [AdventureWorks].[Sales].[SpecialOffer]</H3>' +
               N'<table id="box-table" >' +
               N'<tr><font color="Green"><th>SpecialOfferID</th>
                                  <th>Description</th>
                                  <th>Type</th>
                                  <th>Category</th>
                                  <th>StartDate</th>
                                  <th>EndDate</th>
                                  </tr>' +
               CAST ( (
               
                     SELECT td = CAST([SpecialOfferID] AS VARCHAR(100)),'',
                           td = [Description],'',
                           td = [Type],'',
                           td = [Category] ,'',
                           td = CONVERT(VARCHAR(30),[StartDate],120) ,'',
                           td = CONVERT(VARCHAR(30),[EndDate],120)
                      FROM [AdventureWorks].[Sales].[SpecialOffer]   
                     ORDER BY [SpecialOfferID]
                  FOR XML PATH('tr'), TYPE                   
               Wink AS NVARCHAR(MAX) ) +
               N'</table>'

                              
            EXEC msdb.dbo.sp_send_dbmail @recipients='AnyMailYouWant@SqlIsCool.com',
               @subject = @subject,
               @body = @tableHTML,
               @body_format = 'HTML' ;


Result:

I_Slater
I_Slater
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 29
OH Nice !! .. that does look way better...

At the moment my preview only has a colored background for the Clolumn header
As seen in my Attachment (Dont know if you will be able to see it)
Attachments
email.jpg (288 views, 57.00 KB)
SQL-DBA
SQL-DBA
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 461
Does that work in Gmail? I recall having an issue putting the <head> tag in the body of the email, so I couldn't use CSS. Although I wanted to.
nakache
nakache
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 211
No Need to put <head> tag Smile this is a In-Line CSS in the <body> of the HTML...
BUT Gmail is striping any Style (CSS) out of the HTML... Sad

i will try to make it work in Gmail... googling....
I_Slater
I_Slater
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 29
The one i wrote works for gmail as well, its just a straight forward HTML Email format with no fansy addons.
Its a good alternative if you run into compatability problems.



Keep it simple ...
nakache
nakache
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 211
i fixed my code to support the "problematic" gmail which strips any CSS styling...
and as you say "keep it simple" , so NO bcp, NO xp_cmdshell ,NO Blat = ONLY TSQL Smile

DECLARE @bodyMsg nvarchar(max)
DECLARE @subject nvarchar(max)
DECLARE @tableHTML nvarchar(max)
DECLARE @Table NVARCHAR(MAX) = N''

SET @subject = 'Query Results in HTML with CSS'

SELECT @Table = @Table +'<tr style="background-color:'+CASE WHEN (ROW_NUMBER() OVER (ORDER BY [SpecialOfferID]))%2 =1 THEN '#A3E0FF' ELSE '#8ED1FB' END +';">' +
'<td>' + CAST([SpecialOfferID] AS VARCHAR(100))+ '</td>' +
'<td>' + [Description]+ '</td>' +
'<td>' + [Type]+ '</td>' +
'<td>' + [Category] + '</td>' +
'<td>' + CONVERT(VARCHAR(30),[StartDate],120) + '</td>' +
'<td>' + CONVERT(VARCHAR(30),[EndDate],120) + '</td>' +
'</tr>'
FROM [dbo].[SpecialOffer]   
ORDER BY [SpecialOfferID]

SET @tableHTML =                   
N'<H3><font color="Red">All Rows From [AdventureWorks].[Sales].[SpecialOffer]</H3>' +
N'<table border="1" align="center" cellpadding="2" cellspacing="0" style="color:purple;font-family:arial,helvetica,sans-serif;text-align:center;" >' +
N'<tr style ="font-size: 14px;font-weight: normal;background: #b9c9fe;">
<th>SpecialOfferID</th>
<th>Description</th>
<th>Type</th>
<th>Category</th>
<th>StartDate</th>
<th>EndDate</th></tr>' + @Table +   N'</table>'

         
EXEC msdb.dbo.sp_send_dbmail @recipients='AnyEmail@SqlIsCool.com',
   @subject = @subject,
   @body = @tableHTML,
   @body_format = 'HTML' ;


Result in Gmail:

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search