问题
I am using a trigger on a table to send an email using sp_send_dbmail.
I want to include a file attachment in the email of an image type.
The raw data for the jpeg is stored in the ndl_Image column which is of type binary.
I have the following code:-
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'
This works ok but seems to return the query as a text file if i comment out the last line.
How can I get the attachment as a jpeg file????
Thanks.
回答1:
I don't think this is possible. As specified in the documentation for SP_SEND_DBMAIL:
"When a query is specified, the result set is formatted as inline text. Binary data in the result is sent in hexadecimal format."[emphasis added]
来源:https://stackoverflow.com/questions/3700318/sql-server-sp-send-dbmail-image-file-attachment