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