sp_send_dbmail codifica degli allegati
-
09-06-2019 - |
Domanda
Sto utilizzando sp_send_dbmail in SQL2005 per inviare un'e-mail con i risultati in allegato.Quando l'allegato viene inviato è codificato UCS-2, voglio che sia ANSI o UTF-8.
Ecco l'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
Ho visto alcuni commenti su Internet secondo cui il problema è stato risolto con sql2005 SP2, ma non trovo che sia così.
Soluzione
Penso che l'unico modo per aggirare ciò che vedi sia utilizzare BCP per scaricare i dati in un file flat e quindi allegare quel file.Mi spiace, non potrei essere più d'aiuto.:(
Altri suggerimenti
dopo alcune ricerche su SQL Server 2008 R2:
Aggiungi a sp_send_dbmail @ansi_attachment bit = 0 con eseguire come 'dbo'
sostituire
If (@AttachmentSexist = 1) inizia .......FINE
con:
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
Per fare in modo che il file sia ANSI/UTF-8
alterare sp_send_dbmail che risiede nel file msdb
con questa riga insieme alle altre variabili: @ANSI_Attachment BIT = 0
cioè.
@mailitem_id INT = NULL OUTPUT,
@ANSI_Attachment BIT = 0
WITH EXECUTE AS 'dbo'
e quindi aggiungi questa riga alla chiamata a sp_send_dbmail:
@ansi_attachment = 1
quindi dovrebbe darti un allegato ansi invece di unicode.