Click here to Skip to main content
15,885,056 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have some of records in a table. I want that table records to generate csv file and that file send it to mail using sql server.
Posted

check this

sp_send_dbmail (Transact-SQL)
[^]

sample code

SQL
EXEC sp_send_dbmail @profile_name='default',
@recipients='dev@null.com',
@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 = ','
 
Share this answer
 
v2
Comments
Shweta N Mishra 28-Nov-14 4:11am    
there is everything what you need, Should be able to convert it into your desired requirement. is it first time you are going to write a code for sending mail ?
(chaging the solution little bit , may work for you)

for converting it to csv there is no extra code written.

What you can do is the just a pass the parametr of seperator.
Hi,

How about first converting it to csv and saving it. Look here.

Then you can read the saved file, attach and send mail. Look here

Regards,
Praneet
 
Share this answer
 
Comments
[no name] 28-Nov-14 4:06am    
code behind sql server. What does that mean ? Can you be a bit clear ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900