获取 SQL 数据库邮件以格式化包含 2 列的 HTML 表
我正在使用 SQL Server 2005 并发送 HTML 格式的数据库邮件。我得到了我需要的结果,但我很难按照我想要的方式格式化表格。
正在运行两个单独的 SELECT 语句来填充表行。问题是我希望它们并排显示,因为它们彼此相关,但它们只会将一个显示在另一个之上。我尝试将它们放入一个更大的表中的两个表中,但它不会将其转移。我大约有 10 年没有使用过 HTML,所以这可能不仅仅是脚本本身的问题。
这是包含两个 select 语句的 @body 部分:
N'<table border="1" cellpadding="0" cellspacing="0"><font face="arial">' +
N'<th>Store Number</th>' +
N'<td>'+ CAST ( ( SELECT store_num
FROM store_results
WHERE successful = 'N'
OR successful IS NULL
ORDER BY store_num ASC FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) )+'</td>'+
N'</td>' +
N'<td><table border="1" cellpadding="0" cellspacing="0"><font face="arial">' +
N'<th>Reason</th>' +
N'<td>'+CAST ( ( SELECT
CASE successful
WHEN 'N' THEN 'Failed'
ELSE 'Did Not Run'
END
FROM store_results where successful = 'N' OR successful is null
ORDER BY store_num ASC FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) )+'</td>'+
N'</td>' +
N'</table>
I'm using SQL Server 2005 and am sending a Database Mail formatted in HTML. I am getting the results I need, but I'm having difficulty formatting the table the way I want it.
There are two separate SELECT statements being run that populate the table rows. The problem is that I want them to display side by side as they are in relation to each other, but they will only display one on top of the other. I have tried putting them into two tables within a larger table but it will not shift it over. I haven't used HTML in about 10 years so it's probably a problem with that more than the script itself.
Here is the @body section that contains the two select statements:
N'<table border="1" cellpadding="0" cellspacing="0"><font face="arial">' +
N'<th>Store Number</th>' +
N'<td>'+ CAST ( ( SELECT store_num
FROM store_results
WHERE successful = 'N'
OR successful IS NULL
ORDER BY store_num ASC FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) )+'</td>'+
N'</td>' +
N'<td><table border="1" cellpadding="0" cellspacing="0"><font face="arial">' +
N'<th>Reason</th>' +
N'<td>'+CAST ( ( SELECT
CASE successful
WHEN 'N' THEN 'Failed'
ELSE 'Did Not Run'
END
FROM store_results where successful = 'N' OR successful is null
ORDER BY store_num ASC FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) )+'</td>'+
N'</td>' +
N'</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于明白了这一点。您必须首先设置标题 TD 才能创建列。而不是仅使用格式来使其看起来像您想要的那样。就像这样:
如果您想喜欢它,您实际上可以通过坚持 td/@attribute = case... 来根据查询结果格式化单元格。我最终将所有结果放入 sp 中的临时表中,以保持 html 生成代码更简单。
I finally figured this out. You have to set up the header TDs first to create the columns. Instead of using just format the to have it look how you want. Like so:
You can actually format your cells based on the result of the query by sticking in td/@attribute = case... if you wanted to get fancy with it. I ended up putting all my results into a temp table earlier in the sp to keep the html generation code simpler.