Solved

Using sp_send_dbmail to generate HTML-formatted emails

Posted on 2014-09-26
4
559 Views
Last Modified: 2014-10-07
Have began using sp_send_dbmail in SQL Server 2005 to send me results of a query against one of our process schedule tables as an HTML-formatted email, using the following code :

DECLARE @tableHTML  NVARCHAR(MAX) ;

SET @tableHTML =
    N'<H1>Upcoming Process Schedule List</H1>' +
    N'<table border="1">' +
    N'<tr style="background-color: #5D7B9D; font-weight: bold; color: white;"><th>Year</th><th>Period</th>' +
    N'<th>ScheduledDate</th><th>SalesAnalysis</th>' +
    N'<th>Status</th></tr>' +
    CAST (
    (
    SELECT td = FY,       '',
                    td = PERIOD, '',
                    td = CAST(CREATED_DATE AS VARCHAR(23)), '',
                    td = SALES_COSTS, '',
                    td = STATUS
              FROM [SCHEDULEDB].dbo.tbl_schedules
              WHERE SCHEDULED_DATE >= getdate()
              ORDER BY SCHEDULED_DATE        
              FOR XML PATH('tr'), TYPE
    )
    AS NVARCHAR(MAX) ) +
    N'</table>' ;

EXEC msdb.dbo.sp_send_dbmail @recipients='raymurphy@somewehere.com',
    @subject = 'Upcoming Process Schedule List',
    @body = @tableHTML,
    @body_format = 'HTML' ;

This works fine, and sends me a nicely-formatted HTML email as expected. But out of curiosity, I was wondering what changes I would need to have the table rows formatted as alternate row colours or even if this would be possible using sp_send_dbmail as above ?

Thanks in advance.
0
Comment
Question by:raymurphy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
Ryan McCauley earned 500 total points
ID: 40350104
You could cram a custom style sheet in the beginning of that block to accomplish what you're looking for, all without modifying the code you've already created for the table:

http://www.textfixer.com/tutorials/css-table-alternating-rows.php
0
 

Author Comment

by:raymurphy
ID: 40365465
Sorry for the delay, ryanmccauley - thanks for that, will have a look at incorporating that approach.
0
 

Author Comment

by:raymurphy
ID: 40366061
I've requested that this question be closed as follows:

Accepted answer: 0 points for raymurphy's comment #a40365465

for the following reason:

Looks like this could be useful, so will have a look at incorporating that approach.
0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 40366027
If my post is the direction you end up going with, can you accept my answer as the solution?
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question