Database Mail Formatting Question

Declare @report_file_name varchar(30),
@sql varchar(max),
@emailbody varchar(4000)

      set @sql = N' SET NOCOUNT ON
      EXEC  AdventureWorks.HumanResources.EmployeeGender ''M'''
      PRINT @sql
      SET @report_file_name =  'HumanResources.EmployeeGender' +     convert(varchar(10),getdate(), 112) + '.txt'

SET @emailbody = 'The result from EmployeeHireProcedure as today(  '+ convert(varchar(10),getdate(), 102)

      Exec msdb.dbo.sp_send_dbmail
                        @profile_name = 'AdminAccount',
                        @recipients = '',
                        @subject = 'T-SQL Query Result',
                        @body = @emailbody,
                        @body_format = 'TEXT',
                        @query = @sql,
                        @attach_query_result_as_file = 1,
                        @execute_query_database = 'AdventureWorks',                        
                        @query_attachment_filename = @report_file_name,
                        @query_result_header = 1,
                        @query_result_no_padding = 1,
                        @query_result_separator = '',
                        @query_result_width = 1000;

The Email file I receive is not formatted properly
How can I return this info in a readable format
Possibly a csv file or something like that
This is just a test
Tomorrow I need to output the result of a stored procedure from one of our production databases

Any help would be appreciated
Thanks in advance
Who is Participating?
lcohanDatabase AnalystCommented:
Here, please try this:

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'AdminAccount',--'Taz Profile',
    @recipients = '',
    @query = "SET NOCOUNT ON EXEC  AdventureWorks.HumanResources.EmployeeGender 'M'",
    @attach_query_result_as_file= 1,
    @query_result_no_padding = 1,
    @query_result_separator='      ',
    @subject = 'T-SQL Query Result'

If this is still not ok then you could put the result from your SP in a (temp)table and select from that - you can create a #table and then

INSERT INTO #temp EXEC  AdventureWorks.HumanResources.EmployeeGender ''M''
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.