Quick and Dirty attach a CSV in database mail

Insert your query and away you go :-)

EXEC msdb. dbo.sp_send_dbmail    
@profile_name = '<<NAME>>' ,    
@recipients = '<<RECIPIENTS>>' ,    
@query = '<<QUERY>>' ,      
@attach_query_result_as_file = 1,      
@query_attachment_filename = 'sample.csv' ,     
 @query_result_separator = '   ' ,      
@query_result_header = 1,      
@query_result_no_padding = 1,      
@exclude_query_output = 1,    
@subject = '<<SUBJECT>>'

Leave a Reply

Your email address will not be published. Required fields are marked *