处理从SQL表生成的空HTML表

时间:2015-02-05 14:56:49

标签: html sql sql-server

可能是一个愚蠢的问题。

我正在基于更改SQL Server中的表生成HTML电子邮件。我遇到的问题是,如果这些表中的任何一个为空,则发送整个电子邮件时没有任何内容。

我已经附加了用于填充电子邮件的HTML字段的查询,我认为最好的方法是添加一个案例,如果没有,则在表中插入'N / a'行目标表中的行,但我不确定此代码所需的代码。

或者,任何关于如何编写HTML来处理这些空表的见解都会很有用。

有什么想法?

谢谢!

选择代码:

SELECT 
td = [Exercise],'',
td = [Weight],'',
td = [Set1],'',
td = [Set2],'',
td = [Set3],'',
td = [Set4],'',
td = [Set5],''

FROM [dbo].[Isolation_531]

此表的完整代码:

 SET @tableHTML3 = 
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="Navy">Isolation Work</H3>' +
N'<H4><font color="Navy">This is isolation work based on last week. Feel free to change but try and focus on exercises appropriate to the workout day.</H4>' +
 N'<table id="box-table" >' +
 N'<tr><font color="Green"><th>Name</th>
 <th>Weight</th>
 <th>Set One</th>
 <th>Set Two</th>
 <th>Set Three</th>
 <th>Set Four</th>
 <th>Set Five</th>
 </tr>' +

 CAST ( ( 

SELECT 
td = [Exercise],'',
td = [Weight],'',
td = [Set1],'',
td = [Set2],'',
td = [Set3],'',
td = [Set4],'',
td = [Set5],''

FROM [dbo].[Isolation_531]

 FOR XML PATH('tr'), TYPE 
) AS NVARCHAR(MAX) ) +
 N'</table>' 

1 个答案:

答案 0 :(得分:0)

这是因为当您尝试将null值连接到它时,@ tableHTML3字符串将变为null。有一个feature可以避免这种情况,但将来会被弃用。最好围绕它进行编码。

使用ISNULL

...
CAST (ISNULL('<tr><td colspan="7">NO DATA</td></tr>',( 

SELECT 
td = [Exercise],'',
td = [Weight],'',
td = [Set1],'',
td = [Set2],'',
td = [Set3],'',
td = [Set4],'',
td = [Set5],''

FROM [dbo].[Isolation_531]

 FOR XML PATH('tr'), TYPE 
)) AS NVARCHAR(MAX) )
...

我的HTML有点生疏,但我认为这是对的。随意更新。