23

I am working on ETL and I have this below sql code in my SQL Task in SSIS package. This is how i have coded. I am selecting a data from a table and result of that query as file. I want this attachment sent in CSV format. How do i do it?

EXEC sp_send_dbmail @profile_name='default',
@recipients='[email protected]',
@subject=@SUB,
@body=@BODY,

@query= 'SELECT [MID],[HID],[MeC],[LC],[RowCDate]
FROM [JBC].[dbo].[Table1] WHERE RowCDate >= GETDATE()
',
@attach_query_result_as_file=1

Any help will be very appreciated. Thanks in advance.

3
  • If you are using SSIS then why not use the features of SSIS to create a CSV file and the Send Mail Task to send the email? Commented Mar 4, 2013 at 19:37
  • Based on Requirement. This is the only way have to do it Commented Mar 4, 2013 at 19:40
  • I added an answer below that should format the query result as a CSV and added some more information about the sp_send_dbmail SP. Could you accept the answer if this worked for you? Commented Mar 5, 2013 at 8:22

5 Answers 5

43

Adding @query_result_separator should do the trick.

EXEC sp_send_dbmail @profile_name='default',
@recipients='[email protected]',
@subject=@SUB,
@body=@BODY,

@query= 'SELECT [MID],[HID],[MeC],[LC],[RowCDate]
FROM [JBC].[dbo].[Table1] WHERE RowCDate >= GETDATE()
',
@attach_query_result_as_file = 1,
@query_attachment_filename   = 'Results.csv',
@query_result_separator      = ','

Adding @query_result_no_padding = 1 might clean up the results a bit. All off the arguments can be found here

Sign up to request clarification or add additional context in comments.

4 Comments

The line with @attach_query_result_as_file=1 needs a comma at the end, otherwise the code throws an error.
Please note that these results will not be properly CSV formatted (escaped). If any of your fields contain data with commas, you're in trouble
This answer does not work with opening CSV file in MS Excel. The separator is not enough, but need to specify the separator in the first line in the CSV file as sep=,. Otherwise all the columns will show in one column in MS Excel.
FWIW, I found if your data contains commas then if you set @temp = char(9); and use the sp_send_dbmail parameter: @query_result_separater = @temp; you will have a tab-delimited file. Save it with the '.csv' extension regardless, and Excel will open the file on double-click (rather than having to open Excel then open or import the file and map fields to columns or specify the delimiter).
14
@query='
SET NOCOUNT ON;
select ''sep=;''
select ''Col1'',''Col2'',''Col3'',''Col3''

select CONVERT(NVARCHAR,Col1),ISNULL(Col2, ''''),Col4
FROM ...
SET NOCOUNT OFF;
',

--Additional settings
@query_attachment_filename   = '*.csv',
@query_result_separator      = ';',
@attach_query_result_as_file = 1,
@query_result_no_padding     = 1,
@exclude_query_output        = 1,
@append_query_error          = 0,
@query_result_header         = 0;

2 Comments

Good idea about the NOCOUNT in the @query definition. +1
Inside a proc: SELECT table.myColumn AS [sep=, myColumn] with a normal hard return within the column alias.
7

Adding

'[sep=,' + CHAR(13) + CHAR(10) ColumnName] 

with result solved the issue

See Source

Comments

4

This comment on purple frog indicates you can also use the tab character as a delimiter:

DECLARE @tab char(1) = CHAR(9)
EXEC msdb.dbo.sp_send_dbmail  
@profile_name='donotreply' 
,@recipients ='xx@x'  
,@query= @query  
,@subject= 'xx'  
,@attach_query_result_as_file=1    
,@query_attachment_filename='xx.csv'    
,@query_result_separator=@tab
,@query_result_no_padding=1 –trim
,@query_result_width=32767 –stop wordwrap 

Also looks like this answer's been posted already, my bad: https://stackoverflow.com/a/44315682/5758637

Comments

0

Inside a proc:

SELECT 
table.myColumn AS [sep=, 
myColumn]
, table.myCol2
, table.myCol3...

with a normal hard return within the column alias after "sep=,".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.