SQL Server SP_SEND_DBMAIL图像文件附件

gen*_*ion 5 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'
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

Joe*_*lli 0

我认为这是不可能的。如SP_SEND_DBMAIL文档中所指定:

“当指定查询时,结果集被格式化为内联文本。 结果中的二进制数据以十六进制格式发送。 ”[强调]