Solved

Database Mail Formatting Question

Posted on 2012-03-14
1
361 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now