How to export a csv comma delimited sorted file using SQL Server?

kdbworld
kdbworld used Ask the Experts™
on
I am really struggling trying to automate a script to export a sql server table into a cvs file that can sorted and put in a shared folder.  I have try the export tool in sql server but can't find a way to sort the file.  I also try to BCP the file in to a cvs file and couldn't get that to work.  I know that this can be done.  Please advised.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If it's just a matter of sorting, use a query source instead of a table and sort in the query, selecting all fields.

Then save the SSIS package for executing later, and modify it or schedule it as needed.

Commented:
I would probably do this with a powershell script.  I don't have a sample with me right now but we do this all the time at work to automate exports from sql to csv and/or email.  Once you're done with the script you can execute it from a simple .bat file and schedule the .bat to run whenever you want from the normal windows task scheduler.

Found this with a quick search, may help get you started if you want to give it a shot:
http://beyondrelational.com/modules/2/blogs/908/posts/18005/exporting-from-sql-server-to-csv-file-using-powershell.aspx
Vitor MontalvãoMSSQL Senior Engineer
Distinguished Expert 2017
Commented:
I'm with Richard. In the Export wizard you can provide a query instead of a table, then you just need to provide the query with the ORDER BY clause. Example:
SELECT *
FROM MyTableName
ORDER BY ColumnName

Open in new window

Author

Commented:
I really appreciate Vito Montalvao he really taught me a few things doing this effort.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial