HTML Table with alternative row color, Using SQL XML

Hi,

I want to send out an email and the email body contains a table with data. I am using SQL to create the HTML table and to populate values inside the table. Below is the code I have so far

DECLARE @HTML NVARCHAR(MAX) ;

 

IF (SELECT COUNT(*) from Employee]) != 0

Begin

SET @HTML = '<html><style>

      tr:nth-of-type(even) {

      background-color:#ccc;

    }

</style><body><h1 style="font-family:Tahoma; font-size:12px;">Hi,</h1>' +

'Below is the report'+

'<br></br>'+

'<table border="1" width="100%" style="border:1px solid #77bfe4;font-family:Tahoma; font-weight:normal; font-size:12px;" cellpadding="4" cellspacing="0">' +

'<tr bgcolor="yellow" style="font-family:Tahoma; font-weight:bold; font-size:12px;"><td colspan="2"><center>Report</center></td></tr>'+

'<tr bgcolor="Blue" style="font-family:Tahoma; font-weight:bold; font-size:12px;"><td><center>Col1</center></td><td><center>col2</center></td></tr>' +

CAST((

Select td = col1, '',

td = col2 , '' from

(Select ID as col1, Emp as Col2

from Employee) E

       FOR XML PATH('tr'), TYPE

) AS NVARCHAR(MAX)) + '</table><br>Thank you</br></body></html>'

END

select ISNULL(@HTML,'NoEmail')

But I am having trouble generating alternative colors for each row (tr:nth-of-type(odd) is not working for me)

Below is what the table should look like 

Please help.

Thank you for your help in advance. 

April 27th, 2015 2:49pm

Hi 

The out put Html with that Style looks to be working, here is the working Jsfiddle with your code:

http://jsfiddle.net/g3vyvw5f/

Maybe when they appear in user's email the css is not being executed?

Free Windows Admin Tool Kit Click here and download it now
April 27th, 2015 3:22pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics