sp_send_dbmail 附件编码

发布于 2024-07-04 21:47:08 字数 805 浏览 13 评论 0原文

我在 SQL2005 中使用 sp_send_dbmail 发送一封电子邮件,并将结果放在附件中。 发送附件时,它是 UCS-2 编码的,我希望它是 ANSI 或 UTF-8。

这是 SQL

EXEC msdb.dbo.sp_send_dbmail
    @recipients = '[email protected]'
    , @query = 'DECLARE @string_to_trim varchar(60);SET @string_to_trim = ''1234''; select rtrim(@string_to_trim), ''tom'''
    , @query_result_header=0
    , @subject = 'see attach'
    , @body= 'temp body'
    , @profile_name= N'wksql01tAdmin'
    , @body_format = 'HTML'
    ,@query_result_separator = ','
    ,@query_attachment_filename = 'results.csv'
    ,@query_no_truncate = '0'
    ,@attach_query_result_as_file = 1

我在互联网上看到一些评论说这个问题已在 sql2005 SP2 中修复,但发现情况并非如此。

I am using sp_send_dbmail in SQL2005 to send an email with the results in an attachment. When the attachment is sent it is UCS-2 Encoded, I want it to be ANSI or UTF-8.

Here is the SQL

EXEC msdb.dbo.sp_send_dbmail
    @recipients = '[email protected]'
    , @query = 'DECLARE @string_to_trim varchar(60);SET @string_to_trim = ''1234''; select rtrim(@string_to_trim), ''tom'''
    , @query_result_header=0
    , @subject = 'see attach'
    , @body= 'temp body'
    , @profile_name= N'wksql01tAdmin'
    , @body_format = 'HTML'
    ,@query_result_separator = ','
    ,@query_attachment_filename = 'results.csv'
    ,@query_no_truncate = '0'
    ,@attach_query_result_as_file = 1

I have seen some comments on the internet that this is fixed with sql2005 SP2, but do not find it to be the case.

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

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

发布评论

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

评论(3

花心好男孩 2024-07-11 21:47:08

为了使文件为 ANSI/UTF-8,请

使用此行以及其他变量更改位于 msdb 中的 sp_send_dbmail:@ANSI_Attachment BIT = 0
ie

@mailitem_id INT = NULL OUTPUT,
     @ANSI_Attachment BIT = 0
     WITH EXECUTE AS 'dbo'

然后将此行添加到对 sp_send_dbmail 的调用中:

@ansi_attachment = 1

然后它应该为您提供 ansi 附件而不是 unicode。

In order to have the file be ANSI/UTF-8

alter the sp_send_dbmail that lives in the msdb with this line along with the other variables: @ANSI_Attachment BIT = 0
i.e.

@mailitem_id INT = NULL OUTPUT,
     @ANSI_Attachment BIT = 0
     WITH EXECUTE AS 'dbo'

and then add this line to your call to sp_send_dbmail:

@ansi_attachment = 1

then it should give you an ansi attachment instead of unicode.

书间行客 2024-07-11 21:47:08

我认为解决您所看到的问题的唯一方法是使用 BCP 将数据转储到平面文件,然后附加该文件。 抱歉我无法提供更多帮助。 :(

I think the only way to get around what you are seeing is to use BCP to dump the data to a flat file and then attach that file. Sorry I couldn't be more help. :(

紅太極 2024-07-11 21:47:08

经过对 SQL Server 2008 R2 的一些研究:

  1. 添加到 sp_send_dbmail

    <前><代码>@ANSI_Attachment 位 = 0
    执行为“dbo”

  2. 替换

    <前><代码>IF(@AttachmentsExist = 1)
    开始
    …………
    结尾

    与:

    <前><代码>IF(@AttachmentsExist = 1)
    开始
    如果(@ANSI_Attachment = 1)
    开始
    --将临时附件复制到sysmail_attachments
    插入sysmail_attachments(mailitem_id,文件名,文件大小,附件)
    选择@mailitem_id,文件名,文件大小,
    转换(varbinary(最大),
    substring( -- 从 unicode 中删除 BOM 标记
    转换(varchar(最大),CONVERT(nvarchar(最大),附件)),
    2、数据长度(附件)/2

    来自 sysmail_attachments_transfer
    WHERE uid = @temp_table_uid
    结束否则开始
    --将临时附件复制到sysmail_attachments
    插入sysmail_attachments(mailitem_id,文件名,文件大小,附件)
    选择@mailitem_id,文件名,文件大小,附件
    来自 sysmail_attachments_transfer
    WHERE uid = @temp_table_uid
    结尾
    结尾
    替换

After some research on SQL Server 2008 R2:

  1. Add to sp_send_dbmail:

    @ANSI_Attachment BIT = 0
    WITH EXECUTE AS 'dbo'
    
  2. Replace

    IF(@AttachmentsExist = 1)
        BEGIN
    .......
        END
    

    with:

    IF(@AttachmentsExist = 1)
    BEGIN
        if (@ANSI_Attachment = 1) 
        begin
            --Copy temp attachments to sysmail_attachments      
            INSERT INTO sysmail_attachments(mailitem_id, filename, filesize, attachment)
            SELECT @mailitem_id, filename, filesize, 
                    convert(varbinary(max), 
                        substring( -- remove BOM mark from unicode
                            convert(varchar(max), CONVERT (nvarchar(max), attachment)), 
                            2, DATALENGTH(attachment)/2
                        )
                    )
    
            FROM sysmail_attachments_transfer
            WHERE uid = @temp_table_uid
        end else begin
            --Copy temp attachments to sysmail_attachments      
            INSERT INTO sysmail_attachments(mailitem_id, filename, filesize, attachment)
            SELECT @mailitem_id, filename, filesize, attachment
            FROM sysmail_attachments_transfer
            WHERE uid = @temp_table_uid
        end
    END
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文