如何在HTML电子邮件中设置标头

时间:2018-05-08 14:03:48

标签: sql-server-2008 tsql

这是我上次question/answer留下评论后的后续问题。

我将此代码设为HTML表格,但第一列没有名称,这意味着标题不能正确排列。这是因为我使用这种语法来识别偶数/奇数行 - cast((select ROW_NUMBER() over(order by id) % 2 as 'td',

如何设置第一列的标题?

set  @tableHTML = '<html><head><style>' +
   'td {border: solid black 1px;padding-left:5px;padding-right:5px;padding-top:1px;padding-bottom:1px;font-size:11pt;} ' +
   '</style></head><body>' 
   + '<div style="margin-top:20px; margin-left:5px; margin-bottom:15px; font-weight:bold; font-size:1.3em; font-family:tahoma;">' +
   @textTitle + '</div>' 
   + '<div style="margin-left:50px; font-family:tahoma;"><table cellpadding=0 cellspacing=0 border=0>' +
        '<tr bgcolor=#4b6c9e>' 
            + '<td align=center><font face="calibri" color=White><b>Col1</b></font></td>'     
            + '<td align=center><font face="calibri" color=White><b>Col2</b></font></td>'    
            + '<td align=center><font face="calibri" color=White><b>Col3</b></font></td>'  
            + '<td align=center><font face="calibri" color=White><b>Col4</b></font></td>'   
            + '<td align=center><font face="calibri" color=White><b>Col5</b></font></td>'
            + 
        '</tr>' 

select @body =
   cast((select ROW_NUMBER() over(order by id) % 2 as 'td',
           '',
           isnull(col1,'') as 'td',
           '',     
           isnull(col2,'') as 'td',
           '',      
           isnull(col3,'') as 'td',
           '',
           isnull(col4,'') as 'td',
           '',
           isnull(col5,'') as 'td'        
   from @tableUpdate 
   where notificationType = 'NEWDATE'         
   order by clname
   for XML path('tr'), elements) as nvarchar(max))

select @tableHTML = @tableHTML + @body 
        + '</table></div></body></html>'

1 个答案:

答案 0 :(得分:1)

您的HTML比我强,但这应该工作并允许您在适当的位置插入样式(如果仍然需要)。

select @body =
   cast((select ROW_NUMBER() over(order by id) % 2 as 'td',
           '',
           isnull(col1,'') as 'td',
           '',     
           isnull(col2,'') as 'td',
           '',      
           isnull(col3,'') as 'td',
           '',
           isnull(col4,'') as 'td',
           '',
           isnull(col5,'') as 'td'        
   from @tableUpdate 
   where notificationType = 'NEWDATE'         
   order by clname
   for XML path('tr'), elements) as nvarchar(max))

set @tableHTML = '<html>
                <body>
                    <header>
                        <H3>This would be your header... maybey @textTitle information?</H3>
                        <P>Insert some stuff here if you want</P>
                    </header>
                        <table border = 1>
                        <tr>
                            <th>IDCol</th> 
                            <th>Col1</th> 
                            <th>Col2</th> 
                            <th>Col3/th>
                            <th>Col4</th>
                            <th>Col5</th>
                        </tr>'

select @tableHTML = @tableHTML + isnull(@body,'') + '</table></body></html>'