MSSQL 2008试图找出如何发送多个文件附件与sp_send_dbmail
也许我只是找错了地方。 这里的场景:
我们有存储在varbinary(max)列中的文件。 我想选择这些作为单独的文件,以及附近存储的文件的名称,作为单独的文件附件。
可以这样做吗?
很容易将查询结果作为附件发送,但它只发送1个文件。
感谢
MSSQL 2008试图找出如何发送多个文件附件与sp_send_dbmail
也许我只是找错了地方。 这里的场景:
我们有存储在varbinary(max)列中的文件。 我想选择这些作为单独的文件,以及附近存储的文件的名称,作为单独的文件附件。
可以这样做吗?
很容易将查询结果作为附件发送,但它只发送1个文件。
感谢
基于the docs,这无法做到的。从文件系统连接时,只能附加多个文件(使用@file_attachments=
)。
查询结果始终作为单个文件附加。
我希望这个答案可以帮助其他使用sp_send_dbmail发送多个附件的人。我有一个sql server代理作业作为附件发送查询结果,部分原因是附加的txt文件保持正确的格式/网格。下面是该脚本:
IF (SELECT COUNT(itemnumber) FROM tblItems WHERE date = Convert(date,Convert(varchar(8), getdate(), 112))) > 0
-- add this condition so that when there is no query result, say over the weekend, your client won't receive an empty email.
BEGIN
Declare @currentDate varchar(8), @currentDate2 varchar(10), @filename varchar(100), @subjecttxt varchar(100), @weekdayname varchar(10)
Set @currentDate = Convert(varchar(8), getdate(), 112)
Set @currentDate2 = Convert(varchar(10), getdate(), 101)
Set @weekdayname = datename(dw, @currentDate)
Set @filename = @currentDate + '.txt'
Set @subjecttxt = 'Results for ' + @weekdayname + ' ' + @currentDate2
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'profile_name',
@recipients = '[email protected]',
[email protected]_recipients = '[email protected]',
@blind_copy_recipients = '[email protected]',
@query = 'exec storedprocedure',
@subject = @subjecttxt,
@body = 'mail body.',
@attach_query_result_as_file = 1,
@query_attachment_filename = @filename;
END
然后,我被要求更多的附件添加到相同的电子邮件,这似乎是不可能使用相同的SQL Server代理作业。这是我的策略,作为解决方法。使用相同的作业,添加额外的步骤来运行sqlcmd并输出文件。但是,此举需要运行为操作系统(CmdExec):
sqlcmd -S SQLServerName -dDatabaseName -E -Q "Stored Procedure Name" -o "C:\myFolder\myFile.txt"
您需要先运行此步骤,然后修改的Transact-SQL:
IF (SELECT COUNT(itemnumber) FROM tblItems WHERE date = Convert(date,Convert(varchar(8), getdate(), 112))) > 0
-- add this condition so that when there is no query result, say over the weekend, your client won't receive an empty email.
BEGIN
Declare @currentDate varchar(8), @currentDate2 varchar(10), @filename varchar(100), @subjecttxt varchar(100), @weekdayname varchar(10), @file2 varchar(200)
Set @currentDate = Convert(varchar(8), getdate(), 112)
Set @currentDate2 = Convert(varchar(10), getdate(), 101)
Set @weekdayname = datename(dw, @currentDate)
Set @filename = @currentDate + '.txt'
Set @file2 = 'C:\myFolder\myFile.txt'
Set @subjecttxt = 'Results for ' + @weekdayname + ' ' + @currentDate2
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'profile_name',
@recipients = '[email protected]',
[email protected]_recipients = '[email protected]',
@blind_copy_recipients = '[email protected]',
@query = 'exec storedprocedure',
@subject = @subjecttxt,
@body = 'mail body.',
@attach_query_result_as_file = 1,
@query_attachment_filename = @filename,
@file_attachments = @file2;
END
您可以发送多个附件以这种方式使用只发送单个附件的原始作业。
大鼠。我也这么想。谢谢 – Beta033 2010-03-26 19:27:34