I need to export data to CSV using a stored procedure. I am using the bcp.exe utility to export data
select @sql = 'bcp dbo.Customers out c:\bcp\customers.txt -c -t, -T -S'+ @@servername
exec master..xp_cmdshell @sql
I need to add a header with the column names and a text File Ended‘ at the end of the CSV.
Table structure is like this
ID Name
1 'joe'
2 'jon'
In CSV it should be like this
ID,Name
1,joe
2,jon
END_OF_FILE
I have created a view that will have column header and data
select 'ID' as ID , 'Name' as Name
union all
select ID, Name from Customer
I need to append File Ended at the end of the CSV
Any idea will be appreciated
You’d need some more processing. For example, these DOS commands would create a file
customers2.txtwith a header and footer: