Solved

msdb.dbo.sp_send_dbmail does not work with attachment

Posted on 2014-01-21
5
736 Views
Last Modified: 2014-01-22
Using sql server 2012.

I am using the following code to send query results as attachment to an email. It works in one of my sproc and does not work in the other. The only difference is the query, the attachment file name, subject and body message. bothe are executed under the same user.

Please help me understand why this problem, or suggest alternate solution. This user does not have permission to use xp_cmdshell, and is not sysadmin.

Thank You.

DECLARE @query NVARCHAR(MAX)
	DECLARE @DatePostFix varchar(8) =  CONVERT(VARCHAR(8), GETDATE(), 112)
	Declare @AttachmentFileName varchar(300) = 'Eligible_Customers_'+@DatePostFix+'.csv'
	DECLARE @query_result_separator CHAR(1) = char(9); --comma
	SET @query = 'set nocount on;select * from EligibleCustomers order by survey_designator desc; set nocount off;';
	EXEC msdb.dbo.sp_send_dbmail
	  @profile_name     = 'MyDBAutomatedMailer',
      @recipients       = 'myname@mycompany.org',                                               
      @subject          = 'Monthly Eligible Patients for Press Ganey Rpt.',
      @body             = 'Attached is your Monthly Eligible Customers Rpt.',
      @body_format      = 'HTML',
      @importance       = 'High',
      @query_attachment_filename = @AttachmentFileName,
      @attach_query_result_as_file = 1, 
      @query_result_header = 1, -- include column headers
      @exclude_query_output = 1, -- still seems to include query output (number of rows) & so using set nocount on
      @query_result_width = 1000,  
      @append_query_error = 1,
      @query_result_no_padding = 1, -- do not pad columns
      @query_result_separator = @query_result_separator,
      @query = @query;

Open in new window

0
Comment
Question by:patd1
[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
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39798186
what error do you get?
0
 

Author Comment

by:patd1
ID: 39798246
No.error. I get command executed successfully, but I don't get the email.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39798255
then, as the query is different, maybe the query results is so big that the email is blocked by the email server?
you should check with the email server admins to see if they see the email in some queue...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39798774
No.error. I get command executed successfully, but I don't get the email.
If it was successful you should have seen "Mail Queued".  So you need to find out first why it was not placed on the queue.
0
 

Author Closing Comment

by:patd1
ID: 39799803
I modified the query to select top 100, an ran it for testing. it worked.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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.
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 datatypes, explain the available data types and show how data can be passed into and out of variables.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

756 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