codificação de anexo sp_send_dbmail
-
09-06-2019 - |
Pergunta
Estou usando sp_send_dbmail no SQL2005 para enviar um email com os resultados em anexo.Quando o anexo é enviado, ele é codificado em UCS-2, quero que seja ANSI ou UTF-8.
Aqui está o 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
Tenho visto alguns comentários na internet de que isso foi corrigido com o sql2005 SP2, mas não acho que seja o caso.
Solução
Acho que a única maneira de contornar o que você está vendo é usar o BCP para despejar os dados em um arquivo simples e, em seguida, anexar esse arquivo.Desculpe, não poderia ajudar mais.:(
Outras dicas
após algumas pesquisas no SQL Server 2008 R2:
Adicionar a sp_send_dbmail @ansi_attachment bit = 0 com execução como 'dbo'
substituir
Se (@AttachmentSexist = 1) começar .......FIM
com:
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
Para que o arquivo seja ANSI/UTF-8
altere o sp_send_dbmail que reside no msdb
com esta linha junto com as outras variáveis: @ANSI_Attachment BIT = 0
ou seja
@mailitem_id INT = NULL OUTPUT,
@ANSI_Attachment BIT = 0
WITH EXECUTE AS 'dbo'
e adicione esta linha à sua chamada para sp_send_dbmail:
@ansi_attachment = 1
então ele deve fornecer um anexo ansi em vez de unicode.