Insert sp_send_dbmail attachments

I use sp_send_dbmail in SQL2005 to send emails with the results in the application. When the application is sent, it is encoded by UCS-2, I want it to be ANSI or UTF-8.

Here is SQL

EXEC msdb.dbo.sp_send_dbmail
    @recipients = 'temp@example.com'
    , @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 saw some comments on the Internet that this is fixed with sql2005 SP2, but do not discover that it is.

+3
source share
3 answers

I think the only way to get around what you see is to use BCP to transfer data to a flat file, and then attach this file. Sorry, I could no longer help. :(

0
source

after some research on SQL Server 2008 R2:

  • add to sp_send_dbmail @ANSI_Attachment BIT = 0 WITH EXECUTIVE AS 'dbo'

  • replace

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

:

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
+8

ANSI/UTF-8

change the sp_send_dbmail that lives in msdbwith this line along with 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 the sp_send_dbmail call:

@ansi_attachment = 1

then it should provide you with ansi application instead of unicode.

0
source

Source: https://habr.com/ru/post/1696663/


All Articles