sp_send_dbmail attachment encoding

十年热恋 提交于 2019-12-04 14:43:02

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. :(

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
bushtwig

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!