sp_send_dbmail 将换行符插入附加的 xml 查询结果中

发布于 2024-09-25 03:56:41 字数 939 浏览 0 评论 0原文

当您使用 msdb.dbo.sp_send_dbmail 将查询结果作为附件发送时,您必须提供 @query_result_width 参数,该参数是 10 到 32767 之间的整数。
现在,如果您在最外层的 Select 查询中使用 For XML Path('Row'), Root('Data'), Type,您可能会让 send_dbmail 为您发送 xml 附件在 Microsoft Excel 中打开确实很容易(只需点击 2 或 3 次,这是一个很好的 Excel 表格)
但是,如果您的 xml 文件超过 32767 字节,则 send_dbmail 将在 xml 文件第一行的第 32767 列处插入一个“换行符”,并且它无条件地执行此操作!这意味着它不关心那里写的是什么;它只是打破了线路,因此会损坏你的 xml 数据...... 我已经在互联网上闲逛了 5 个多小时,但对于如何防止 sp_send_dbmail 注入讨厌的换行符没有任何积极的结果。 我遇到的唯一解决方案是在我有控制权的地方注入换行符,并且它不在第 32767 列之后。像下面这样的查询就可以解决问题,但这是额外的工作而且不干净......

Select Replace(
    Cast(
        (
            Select
                *
            From Table
            For XML Path('Row'), Root('Data'), Type
        )
    As NVarChar(Max))
, N'/>', N'/><!--This is the controlled line-break. Remove this comment-->
')

有人有更好的主意吗?我在这里错过了什么吗?
我使用 SQL Server 2008 Enterprise (x64)

When you use msdb.dbo.sp_send_dbmail to send a query result as an attachment, you'll have to provide the @query_result_width parameter which is an integer between 10 and 32767.
Now, If you use For XML Path('Row'), Root('Data'), Type in your most-outer Select query, you may get the send_dbmail to send an xml attachment for you which is really easy to open in Microsoft Excel (just 2 or 3 clicks and it's a nice excel table)
But, if your xml file exceeds the 32767 bytes, the send_dbmail WILL insert a "line-break" at 32767th column of the first line of xml file, and it does this UNCONDITIONALLY! It means it doesn't care what's written there; it just breaks the line and therefore WILL corrupt your xml data...
I've been wandering the internet for more than 5 hours with no positive result on how should I prevent sp_send_dbmail from injecting that nasty line-break.
The only solution I came across was injecting the line-break where I have the control and it's not after 32767th column. A query like the following will do the trick, but it's extra work and not clean...

Select Replace(
    Cast(
        (
            Select
                *
            From Table
            For XML Path('Row'), Root('Data'), Type
        )
    As NVarChar(Max))
, N'/>', N'/><!--This is the controlled line-break. Remove this comment-->
')

Anybody has any better idea? Am I missing something here?
I use SQL Server 2008 Enterprise (x64)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文