SQL Server SP_SEND_DBMAIL图像文件附件

时间:2010-09-13 12:22:55

标签: 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文件????

感谢。

1 个答案:

答案 0 :(得分:0)

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

  

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

相关问题