sql-serversp-send-dbmail

Comma delimiter is not being respected


I am sending results of a query via sp_send_dbmail as a comma-delimited file. The results are accurate and the mail is sent with a csv attachment.

Excel, however, is not respecting the delimiter. The results for each data row are crammed into the first cell (Column A) of each row rather than allocated to each column based on the delimiter. What am I missing here?

    -- Build the mail to send.
    exec msdb.dbo.sp_send_dbmail 
    @recipients = @employeeEmail
    ,@from_address = @senderaddress
    ,@query = @cmd
    ,@body = 'Here are the wage changes for the previous month.'  
    ,@subject = 'Wage Changes Over the Past Month'
    ,@attach_query_result_as_file = 1
    ,@query_attachment_filename = 'WageChangesReport.csv'
    ,@query_result_width = 32767
    ,@query_result_separator = ','
    ,@query_result_no_padding = 1

Solution

  • The answer provided by Christian in this thread (regarding 'Sep=') solved the problem.

    set delimiter when using sp_send_dbmail (csv file)

    declare @cmd varchar(100) = 'print ''sep=;'' exec InterfaceDev..GatherPayChanges ''' + @date1 + ''', ''' + @date2 + ''''