Format Excel Attachment using sp_send_dbmail

  • I would like to have the excel file sent via dbmail formatted to be nice an pretty so that the receiver does not have to run a macro against it. Any clues on how to do this would be appreciated. A code snipped is included below.

    EXEC msdb.dbo.sp_send_dbmail @profile_name = @mailprofile

    ,@recipients = @mailrecipient

    ,@subject = @mailsubject

    ,@body_format = 'HTML'

    ,@body = @mailtext

    ,@query = @sqlstatement

    ,@attach_query_result_as_file = 1

    ,@query_result_no_padding = 1

    ,@query_attachment_filename = @mailfilename

    ,@query_result_width = 1000 -- set query result width

    ,@query_result_separator ='' --this is a tab in between the quotes not a space

    ,@append_query_error = 1

  • You can BCP that to a CSV file and add it to your email as an attachment.

    Or, rewrite it with SSIS and it will do everything you wanted.

  • Ray,

    Thanks for the information. I will try BCP to obtain what I need.

  • You are welcome. Let me know how it turns out.

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

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