How to send mails using sp_send_dbmail with more than one query result attachments?

  • Hi,

    Below is the query i used in SQL2005 to send a mail with an txt attachment which had the result of a query.

    EXEC @statusMail = [MSDB].dbo.sp_send_dbmail @profile_name = 'dbmail',

    @recipients = @ToemailID

    ,@copy_recipients = @CCEmailID

    ,@blind_copy_recipients = @BCCEmailID

    ,@query = @sqlquery

    ,@subject='XYZ'

    ,@body = XYZ'

    ,@query_attachment_filename ='XYZ.txt'

    ,@attach_query_result_as_file = 1

    ,@query_result_width = 250

    Though it worked out fine, now, i need to send 2 files as attachments in the same mail both the attachements are query results. How can this be done in SQL2008? If i have the files as images in a DB table, would it make it any easier?

    Any help??!!!

    Thanks in advance 🙂

  • I don't think this is possible with sp_send_mail. What about CLR?

  • You could try putting the two queries into a procedure, and then specifying to execute that procedure as the query. That may do the trick.

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2

  • I dont think using an SP would solve the issue as i need to send 2 attachments in the email

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply