SQL Clone
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
Lowell
Lowell
SSC Guru
SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)SSC Guru (285K reputation)

Group: General Forum Members
Points: 285906 Visits: 41888
thakur.bhagat24 (12/20/2016)
In previous example is very useful
But I have one new requirement of order by color what we get using below case.

CASE WHEN (ROW_NUMBER() OVER (ORDER BY [SpecialOfferID]))%2 =1 THEN '#A3E0FF' ELSE '#8ED1FB' END


i.e order by '#A3E0FF' OR

i.e Order by '#8ED1FB'

you can use Cascading Style Sheets and the nth-child to set the row color of every other row automatically:
the

<style TYPE="text/css">
tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
</STYLE>



Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Cuneyd Tanriverdi
Cuneyd Tanriverdi
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 90
nakache - Thursday, June 20, 2013 1:35 AM
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 ) AS NVARCHAR(MAX) ) + N'</table>' EXEC msdb.dbo.sp_send_dbmail @recipients='AnyMailYouWant@SqlIsCool.com', @subject = @subject, @body = @tableHTML, @body_format = 'HTML' ;Result:

nakache, thanks for your help, I did not know SQL supports for CSS. I was editing all the HMTL tags from the beginning for each code. This saved my time bro.

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