4
我在表上使用sp_send_dbmail发送电子邮件时使用触发器。SQL Server SP_SEND_DBMAIL图像文件附件
我想在图像类型的电子邮件中包含文件附件。
jpeg的原始数据存储在二进制类型的ndl_Image列中。
我有下面的代码: -
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 #'[email protected]
SET @Q = 'SELECT ndl_Image from dbo.ndl_data where ndl_ID ='[email protected]
--Execute the stored procedure to send mail.
EXEC msdb.dbo.sp_send_dbmail
--Pass it the following paramaters.
@[email protected],
@[email protected],
@[email protected],
@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文件?
谢谢。
我想你可以通过首先使用BCP导出到文件系统来实现同样的想法。尽管没有像从Varbinary那里获得正确的优雅,但最终目标仍然可以实现。我在这里发布了一些示例代码作为概念:http://stackoverflow.com/a/12432031/261997 – RThomas 2012-09-14 21:48:08