file allegato Immagine SQL Server sp_send_dbmail
-
02-10-2019 - |
Domanda
Sto usando un innesco su un tavolo per inviare una e-mail sp_send_dbmail.
voglio includere un file allegato nel messaggio di posta elettronica di un tipo di immagine.
I dati grezzi per la jpeg è memorizzato nella colonna ndl_Image che è di tipo binario.
Ho il seguente codice: -
DECLARE @ReferenceID varchar(max)
DECLARE @Recipient varchar(Max)
DECLARE @Body varchar(max)
DECLARE @Subject varchar(max)
DECLARE @Q varchar(max)
--Get the EntryId and FormID for the inserted data.
SET @ReferenceID = 40
SET @Recipient = (SELECT ndl_CategorySendTo FROM ndl_config WHERE ndl_CategoryName = 'Dead Animal')
SET @Body = '<html>A new request has been created.</html>'
SET @Subject = 'NDL Report It: New Request #'+@ReferenceID
SET @Q = 'SELECT ndl_Image from dbo.ndl_data where ndl_ID ='+@ReferenceID
--Execute the stored procedure to send mail.
EXEC msdb.dbo.sp_send_dbmail
--Pass it the following paramaters.
@recipients=@Recipient,
@body=@Body,
@subject=@Subject,
@profile_name='NDLProfile',
@body_format ='HTML',
@execute_query_database='NDL_MX',
@query = @Q,
@attach_query_result_as_file = 1,
@query_attachment_filename = 'image.jpg'
Questo funziona bene, ma sembra tornare la query come un file di testo, se io commento l'ultima riga.
Come posso ottenere l'allegato come file jpeg ????
Grazie.
Soluzione
Non credo che questo sia possibile. Come specificato nella documentazione per sp_send_dbmail :
"Quando si specifica una query, il risultato set è formattato come testo in linea. Dati binari nel risultato viene inviato in formato esadecimale. "[enfasi aggiunta]