• sandosh.star (2/12/2013)


    @subject ='Status Log for Today'

    ,@profile_name ='Status'

    ,@query ='my query'

    ,@attach_query_result_as_file = 1

    ,@query_attachment_filename ='Status.csv'

    output has Column name called COUNTRY contain different countries AU,MY,SG

    I want to group by country and attach the result in different sheet in same excel file

    Example: If AU means all AU data in one sheet and SG means all SG data in another sheet

    Is it possible?

    No, for multiple reasons. 1) a CSV file and an Excel file are two different things. While a file with a .csv extension may open in Excel by default because of a Windows setting on somone's workstation it does not make that file an Excel-formatted file. A CSV file is merely a special form of a plain-text file and cannot have "Tabs". 2) msdb.dbo.sp_send_dbmail cannot generate Excel files. It can generate text files with whatever name, extension and delimiter you like, but not Excel-formatted files. 3) msdb.dbo.sp_send_dbmail can only execute one query and push all the results from that query into a text file.

    If you must generate an Excel-formatted file by issuing multiple queries and pushing each result into a separate Worksheet within an Excel Workbook then you could look into using SSIS.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato