将 SQL 查询结果表转换为电子邮件的 HTML 表
我正在运行一个返回结果表的 SQL 查询。我想使用 dbo.sp_send_dbMail 通过电子邮件发送该表。
SQL 中有没有一种直接的方法可以将表格转换为 HTML 表格?目前,我正在使用 COALESCE 手动构建它,并将结果放入我用作 emailBody 的 varchar 中。
有更好的方法吗?
I am running a SQL query that returns a table of results. I want to send the table in an email using dbo.sp_send_dbMail.
Is there a straightforward way within SQL to turn a table into an HTML table? Currently, I'm manually constructing it using COALESCE and putting the results into a varchar that I use as the emailBody.
Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是我的实现:任何查询结果到 html 表。
我正在创建一些辅助程序来实现这一目标。这些帮助程序非常灵活,并且可以在各种上下文中重用。
fnValidateDynamicSql
- 验证传递的动态语句spAlterTblByRs
- 将任何 SQL 语句结果保存到#table
。允许从代码spQueryResultAsHtmlTable
中完全删除动态 SQL - 从任何传递的 SQL 语句创建 html 表享受:)
Here is my implementation: any query result to html table.
I'm creating some helper procedures to achieve this. These helper procedures are flexible, and may be reused in various contexts.
fnValidateDynamicSql
- to validate passed dynamic statementspAlterTblByRs
- to save any SQL statement result to#table
. Allows to completely remove dynamic SQL from the codespQueryResultAsHtmlTable
- creates html table from any passed SQL statementEnjoy :)
我制作了一个动态过程,它将任何随机查询转换为 HTML 表,因此您不必像其他响应中那样对列进行硬编码。
用法:
相关:这里是类似的代码,可将任意查询转换为 CSV 字符串。
I made a dynamic proc which turns any random query into an HTML table, so you don't have to hardcode columns like in the other responses.
Usage:
Related: Here is similar code to turn any arbitrary query into a CSV string.
这是一篇标题为“将查询输出格式化为 HTML 表 - 最简单的方法[存档]”。您需要用自己的查询详细信息替换本示例中的查询详细信息,该示例获取表列表和行计数。
一旦您拥有
@body
,您就可以使用您想要的任何电子邮件机制。Here is one way to do it from an article titled "Format query output into an HTML table - the easy way [archive]". You would need to substitute the details of your own query for the ones in this example, which gets a list of tables and a row count.
Once you have
@body
, you can then use whatever email mechanism you want.这可能会给你一些想法——
This might give you some idea --
下面是我常用的脚本。我使用它通过 SQL 作业在两个表/视图上运行脚本,并通过邮件将结果作为两个 HTML 表发送。当然,您应该在运行之前创建邮件配置文件。
Here my common used script below. I use this for running scripts on two tables/views with SQL job and send results as two HTML tables via mail. Ofcourse you should create mail profile before run this.
JustinStolle 的答案以不同的方式。一些注意事项:
print
语句可能会将字符串截断为 4000 个字符,但我的测试字符串的长度为 9520 个字符。[tr/th]
表示层次结构,例如...
。[@name]
将字段添加为 XML 属性。null
可以防止这种情况发生。JustinStolle's answer in a different way. A few notes:
print
statement may truncate the string to 4000 characters, but my test string for example was 9520 characters in length.[tr/th]
indicates hierarchy, e.g.,<tr><th>...</th></tr>
.[@name]
adds fields as XML attributes.null
in between fields prevents that.基于 JustinStolle 代码(谢谢),我想要一个通用的解决方案,而无需指定列名。
该示例使用临时表的数据,但当然可以根据需要进行调整。
这是我得到的:
based on JustinStolle code (thank you), I wanted a solution that could be generic without having to specify the column names.
This sample is using the data of a temp table but of course it can be adjusted as required.
Here is what I got:
假设有人找到了这里的方法并且不理解标记的答案 SQL 的用法,请阅读我的...它已编辑并且可以工作。表:员工,列:员工姓名、员工电话和员工出生日期
Suppose someone found his way here and does not understand the usage of the marked answer SQL, please read mine... it is edited and works. Table:staff, columns:staffname,staffphone and staffDOB
我尝试使用上面的 Mahesh 示例打印多个表。发帖是为了方便别人
I tried printing Multiple Tables using Mahesh Example above. Posting for convenience of others
所有其他答案都使用变量和 SET 操作。这是在 select 语句中执行此操作的一种方法。只需将其作为现有选择中的一列即可。
All the other answers use variables and SET operations. Here's a way to do it within a select statement. Just drop this in as a column in your existing select.
接下来的一段代码,我准备生成用于文档的 HTML 文件,其中包括每个表中的表名称和用途以及表元数据信息。这可能会有帮助!
Following piece of code, I have prepared for generating the HTML file for documentation which includes Table Name and Purpose in each table and Table Metadata information. It might be helpful!