Sonali Avhad

Sonali Avhad

  • NA
  • 234
  • 39.6k

send email with excel attachment using sql stored procedure

Feb 1 2021 6:04 AM
I using sql stored procedure for sending email with attachment.
 
I want to send email with excel attachment.
 
I used the following code
  1. DECLARE @Body VARCHAR(8000)  
  2. DECLARE @Qry varchar(8000)  
  3. SET @Body ='Hi PFA,'  
  4. SET @Qry='set nocount on;select * from tblname'  
  5. DECLARE  
  6. @tab char(1) = CHAR(9)  
  7. EXEC msdb.dbo.sp_send_dbmail  
  8. @profile_name = 'Profilename' ,  
  9. @recipients = 'receipentemail',  
  10. @body= @Body,  
  11. @query=@Qry,  
  12. @subject='Send db mailer with attachment',  
  13. @attach_query_result_as_file = 1,  
  14. @query_attachment_filename='Report.xls'  
but in excel is not proper it is disturbed columns and rows.
 
Please help me for proper excel attachment.

Answers (1)