sql-server – SQL Server SP_SEND_DBMAIL映像文件附件

我在表上使用触发器使用sp_send_dbmail发送电子邮件.

我想在图像类型的电子邮件中包含文件附件.

jpeg的原始数据存储在ndl_Image列中,该列的类型为binary.

我有以下代码: –

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'

这工作正常,但如果我注释掉最后一行,似乎将查询作为文本文件返回.

如何将附件作为jpeg文件获取????

谢谢.

最佳答案
我不认为这是可能的.如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]

转载注明原文:sql-server – SQL Server SP_SEND_DBMAIL映像文件附件 - 代码日志