Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Database Mail Formatting Question

Posted on 2012-03-14
1
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 40

Accepted Solution

by:
lcohan earned 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

704 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