获取 SQL 数据库邮件以格式化包含 2 列的 HTML 表

发布于 2024-10-21 13:23:32 字数 1217 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-10-28 13:23:32

我终于明白了这一点。您必须首先设置标题 TD 才能创建列。而不是仅使用格式来使其看起来像您想要的那样。就像这样:

N'<table border="1" cellpadding="0" cellspacing="0"><font face="arial">' +
N'<tr><td>Store Number</td><td>Reason</td></tr>' +
N'<td>'+ CAST ( ( SELECT 
    td = store_num
    td = reason
        from store_results
        ORDER BY store_num ASC FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) +  
N'</table>

如果您想喜欢它,您实际上可以通过坚持 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:

N'<table border="1" cellpadding="0" cellspacing="0"><font face="arial">' +
N'<tr><td>Store Number</td><td>Reason</td></tr>' +
N'<td>'+ CAST ( ( SELECT 
    td = store_num
    td = reason
        from store_results
        ORDER BY store_num ASC FOR XML PATH('tr'), TYPE ) AS NVARCHAR(MAX) ) +  
N'</table>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文