MSSQL 2008
Maybe i'm just looking in the wrong place. Here's the scenario:
We have files stored in a varbinary(max) column. I'd like to select these as individual files, along with the names of the files stored nearby, as individual file attachments.
can this be done?
it's easy enough to send the results of a query as an attachment, but it just sends 1 file.
Thanks
Based on the docs, this can't be done. You can only attach multiple files when they attached from the file system (using
@file_attachments=
).Query results are always attached as a single file.
I hope this answer may help others who use sp_send_dbmail to send multiple attachments. I have a sql server agent job to send the query result as an attachment, part of the reasons is that the attached txt file keeps the right format/grid. Here is the script:
A couple of months later, I was asked to add more attachments to the same email, which seems not possible to use the same sql server agent job. Here is my strategy as a workaround. Basically you'll need to add an additional step to run sqlcmd and output the file. But the step needs to be run as Operating system (CmdExec):
Run this step first, then modify the Transact-SQL. You can also run sqlcmd as a batch file.
Now you can send multiple attachments using your original job which only sends a single attachment.