Solved

Database Mail Formatting Question

Posted on 2012-03-14
1
363 Views
Last Modified: 2012-08-14
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(yyyy.mm.dd):  '+ convert(varchar(10),getdate(), 102)

      Exec msdb.dbo.sp_send_dbmail
                        @profile_name = 'AdminAccount',
                        @recipients = 'xxxx@gmail.com',
                        @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
0
Comment
Question by:VitaminD
1 Comment
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 37726342
Here, please try this:

EXEC msdb.dbo.sp_send_dbmail
    @profile_name = 'AdminAccount',--'Taz Profile',
    @recipients = 'xxxx@gmail.com',
    @query = "SET NOCOUNT ON EXEC  AdventureWorks.HumanResources.EmployeeGender 'M'",
    @attach_query_result_as_file= 1,
    @query_attachment_filename='HumanResources_EmployeeGender.csv',
    @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''
SELECT * FROM #temp
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question